Solving in Ruby, Perl and Java AtCoder ABC 113 C Reference

Introduction

This theme

AtCoder Beginner Contest 113 C - ID Difficulty: 877

This theme, reference Ruby It's a problem that I solved before, * AtCoder Judge System Update Test Contest 202004 B * is a little difficult.

ruby.rb


n, m = gets.split.map(&:to_i)
p = []
m.times do |i|
  y = gets.split.map(&:to_i)
  p[i] = [i, y, ""]
end
p.sort! {|a, b| (a[1][0]<=>b[1][0]).nonzero? || (a[1][1]<=>b[1][1])}
pref = 0
cnt = 1
m.times do |i|
  if pref == p[i][1][0]
    cnt += 1
  else
    cnt = 1
    pref = p[i][1][0]
  end
  p[i][2] = p[i][1][0].to_s.rjust(6, "0").concat(cnt.to_s.rjust(6, "0"))
end
p.sort! {|a, b| a[0]<=>b[0]}
m.times do |i|
  puts p[i][2]
end

array.rb


  p[i] = [i, y, ""]
  # => [[0, [1, 32], ""], [1, [2, 63], ""], [2, [1, 12], ""]]
  p.sort! {|a, b| (a[1][0]<=>b[1][0]).nonzero? || (a[1][1]<=>b[1][1])}
  # => [[2, [1, 12], ""], [0, [1, 32], ""], [1, [2, 63], ""]]
  p[i][2] = p[i][1][0].to_s.rjust(6, "0").concat(cnt.to_s.rjust(6, "0"))
  # => [[2, [1, 12], "000001000001"], [0, [1, 32], "000001000002"], [1, [2, 63], "000002000001"]] 

If the inside of the array p comes to mind, then this level of problem is graduation.

Ruby (Hash version)

RubyHash.rb


n, m = gets.split.map(&:to_i)
p = []
h = Hash.new(0);
m.times do |i|
  y = gets.split.map(&:to_i)
  p[i] = [i, y, ""]
  h[y[0]] +=1
end
p.sort! {|a, b| b[1][1]<=>a[1][1]}
m.times do |i|
  p[i][2] = p[i][1][0].to_s.rjust(6, "0").concat(h[p[i][1][0]].to_s.rjust(6, "0"))
  h[p[i][1][0]] -= 1
end
p.sort! {|a, b| a[0]<=>b[0]}
m.times do |i|
  puts p[i][2]
end

We are solving using a hash instead of a count variable. It is a little faster because the code length is shorter and the sorting condition is one. ~~ The Java version is TLE, so I added it. ~~ Perl

perl.pl


chomp (my ($n, $m) = split / /, <STDIN>);
my @p;
for my $i (0..$m-1) {
  chomp (my @in = split / /, <STDIN>);
  $p[$i] = [$i, @in];
}
@p = sort {$$a[1]<=>$$b[1] || $$a[2]<=>$$b[2]} @p;
my ($pref, $cnt) = (0, 0);
for my $i (0..$m-1) {
  if ($pref == $p[$i][1]) {
    $cnt++;
  } else {
    $pref = $p[$i][1];
    $cnt = 1;
  }
  $p[$i][3] = sprintf("%06s", $p[$i][1]) . sprintf("%06s", $cnt);
}
@p = sort {$$a[0]<=>$$b[0]} @p;
print $$_[3], "\n" for @p;

This is also a multi-condition sort by applying * previous article *.

array.pl


[0, 1, 32], [1, 2, 63], [2, 1, 12]

However, the structure in the array is different from * Ruby *, and it is flat. Java

java.java


import java.io.PrintWriter;
import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int m = Integer.parseInt(sc.next());
        Map<Integer, Integer> h = new HashMap<>();
        List<City> city = new ArrayList<>();
        for (int i = 0; i < m; i++) {
            int Pref = Integer.parseInt(sc.next());
            int Year = Integer.parseInt(sc.next());
            city.add(new City(i, Pref, Year));
            if (h.containsKey(Pref)) {
                h.put(Pref, h.get(Pref) + 1);
            } else {
                h.put(Pref, 1);
            }
        }
        sc.close();
        city.sort((o1, o2) -> Integer.compare(o2.Year, o1.Year));
        for (City c : city) {
            c.Num = h.get(c.Pref);
            h.put(c.Pref, h.get(c.Pref) - 1);
        }
        city.sort((o1, o2) -> Integer.compare(o1.ID, o2.ID));
        PrintWriter pw = new PrintWriter(System.out);
        for (City c : city) {
            pw.printf("%06d%06d\n", c.Pref, c.Num);
        }
        pw.flush();
    }

    static class City {
        int ID;
        int Pref;
        int Year;
        int Num;

        City(int ID, int Pref, int Year) {
            this.ID = ID;
            this.Pref = Pref;
            this.Year = Year;
            this.Num = 0;
        }
    }
}

flush.java


import java.io.PrintWriter;

If you do not use PrintWriter, it will be TLE.

Ruby Ruby(Hash) Perl Java
Code length 450 Byte 372 Byte 470 Byte 1444 Byte
Execution time 943 ms 880 ms 1079 ms 1615 ms
memory 27376 KB 33900 KB 45168 KB 107864 KB

Summary

Referenced site instance method String#rjust

Recommended Posts

Solving in Ruby, Perl and Java AtCoder ABC 113 C Reference
Solving with Ruby, Perl and Java AtCoder ABC 128 C
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 1)
Sorting AtCoder ABC 111 C hashes to solve in Ruby, Perl and Java
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 2) Dynamic programming
Solving with Ruby, Perl and Java AtCoder ABC 136 D Breadth-first search
AtCoder ARC 081 C hash to solve in Ruby, Perl and Java
Solving with Ruby and Java AtCoder ABC129 D 2D array
AtCoder dwango Programming Contest B in Ruby, Perl and Java
AtCoder ABC 169 C Floating Point Fits in Ruby
Solving with Ruby and Crystal AtCoder ABC 129 D
ABC177 --solving E in Ruby
Java Direction in C ++ Design and Evolution
Java to C and C to Java in Android Studio
Differences in writing Java, C # and Javascript classes
atcoder ABC113 C problem
atcoder ABC115 C problem
Organize builds in C ++ / Java and Win / Linux combinations
Math Girls Secret Note 104th implemented in Ruby and C
Ruby C extension and volatile
Reproduce Java enum in C #
Differences in how to handle strings between Java and Perl
C # and Java Overrides Story
Solving with Ruby AtCoder ACL Beginner Contest C Union Find (DSU)
Solve AtCoder Beginner Contest 151 in java
Solve AtCoder Beginner Contest 150 in java
Read Java properties file in C #
Encoding and Decoding example in Java
Solve AtCoder Beginner Contest 153 in java
Java, JavaScript, C # (difference in assignment)
Basic data types and reference types (Java)
Java reference mechanism (stack and heap)
CGI in C and Dart: Introduction (1)
[Ruby] then keyword and case in
StringBuffer and StringBuilder Class in Java
Solve AtCoder Beginner Contest 175 in java
Write keys and values in Ruby
Java pass by value and pass by reference
Solve AtCoder Beginner Contest 160 in java
Understanding equals and hashCode in Java
Solve AtCoder Beginner Contest 152 in java
About Java primitive types and reference types
Java basic data types and reference types
Solve AtCoder Beginner Contest 156 in java
Encrypt with Java and decrypt with C #
AtCoder Beginner Contest 167 C Problem (Java)
Hello world in Java and Gradle
Solve ARC104 D Multiset Mean with Scala, Java, C ++, Ruby, Perl, Elixir
About returning a reference in a Java Getter
Solve with Ruby AtCoder ABC177 D UnionFind
Difference between final and Immutable in Java
[Easy-to-understand explanation! ] Reference type type conversion in Java
Java reference to understand in the figure
java core: HotSpot compiler and C heap
Make bubble sort and selection sort in Ruby
Differences in writing in Ruby, PHP, Java, JS
Link Java and C ++ code with SWIG
[Java] for Each and sorted in Lambda
Summary of hashes and symbols in Ruby
[Ruby] Classification and usage of loops in Ruby
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization