Sorting AtCoder ABC 111 C hashes to solve in Ruby, Perl and Java

Introduction

This theme

AtCoder Beginner Contest 111 C - //// Difficulty: 822

This theme, hash sorting Ruby Duplicate elements, it's a hash, isn't it? The solution is similar to the previous * AtCoder ABC 113 C in Ruby, Perl and Java *.

ruby.rb


n = gets.to_i
v = gets.split.map(&:to_i)
p = Hash.new(0)
q = Hash.new(0)
n.times do |i|
  p[v[i]] += 1 if i.even?
  q[v[i]] += 1 if i.odd?
end
ps = Hash[p.sort_by{|k, v| -v}].keys
qs = Hash[q.sort_by{|k, v| -v}].keys
x, y, ans = 0, 0, n / 2
f = false
ps.size.times do |i|
  x = ps[i]
  qs.size.times do |j|
    if x != qs[j]
      y = qs[j]
      f = true
      break
    end
  end
  break if f
end
ans = n - p[x] - q[y] if f
x, y = 0, 0
f = false
qs.size.times do |i|
  x = qs[i]
  ps.size.times do |j|
    if x != ps[j]
      y = ps[j]
      f = true
      break
    end
  end
  break if f
end
ans = n - q[x] - p[y] if f && ans > n - q[x] - p[y]
puts ans

hash.rb


ps = Hash[p.sort_by{|k, v| -v}].keys

In ABC113C, it was a * Perl * -like sort, but this time I tried a * Ruby * -like sort. Perl

perl.pl


chomp (my $n = <STDIN>);
chomp (my @v = split / /, <STDIN>);
my (%p, %q);
for my $i (0..$n-1) {
  if ($i % 2 == 0) {
    $p{$v[$i]}++;
  } else {
    $q{$v[$i]}++;
  }
}
my @ps = sort {$p{$b}<=>$p{$a}} keys %p;
my @qs = sort {$q{$b}<=>$q{$a}} keys %q;
my ($x, $y) = (0, 0);
my $ans = $n / 2;
my $f = 0;
for my $i (0..$#ps) {
  $x = $ps[$i];
  for my $j (0..$#qs) {
    if ($x != $qs[$j]) {
      $y = $qs[$j];
      $f = 1;
      last;
    }
  }
  last if $f;
}
$ans = $n - $p{$x} - $q{$y} if $f;
($x, $y) = (0, 0);
my $f = 0;
for my $i (0..$#qs) {
  $x = $qs[$i];
  for my $j (0..$#ps) {
    if ($x != $ps[$j]) {
      $y = $ps[$j];
      $f = 1;
      last;
    }
  }
  last if $f;
}
$ans = $n - $q{$x} - $p{$y} if $f && $ans > $n - $q{$x} - $p{$y};
print "$ans\n";

hash.pl


my @ps = sort {$p{$b}<=>$p{$a}} keys %p;

java.java


import java.util.*;
import java.util.stream.Collectors;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        Map<Integer, Integer> p = new HashMap<>();
        Map<Integer, Integer> q = new HashMap<>();
        for (int i = 0; i < n; i++) {
            int v = Integer.parseInt(sc.next());
            if (i % 2 == 0) {
                if (p.containsKey(v)) {
                    p.put(v, p.get(v) + 1);
                } else {
                    p.put(v, 1);
                }
            } else {
                if (q.containsKey(v)) {
                    q.put(v, q.get(v) + 1);
                } else {
                    q.put(v, 1);
                }
            }
        }
        sc.close();

        Map<Integer, Integer> ps = p.entrySet().stream()
                .sorted(Map.Entry.<Integer, Integer>comparingByValue().reversed()
                        .thenComparing(Map.Entry.comparingByKey()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
        Map<Integer, Integer> qs = q.entrySet().stream()
                .sorted(Map.Entry.<Integer, Integer>comparingByValue().reversed()
                        .thenComparing(Map.Entry.comparingByKey()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));

        List<Integer> pss = new ArrayList<>(ps.keySet());
        List<Integer> qss = new ArrayList<>(qs.keySet());
        int x = 0, y = 0, ans = n / 2;
        boolean f = false;
        for (int i = 0; i < pss.size(); i++) {
            x = pss.get(i);
            for (int j = 0; j < qss.size(); j++) {
                if (x != qss.get(j)) {
                    y = qss.get(j);
                    f = true;
                    break;
                }
            }
            if (f)
                break;

        }
        if (f)
            ans = n - p.get(x) - q.get(y);
        x = 0;
        y = 0;
        f = false;
        for (int i = 0; i < qss.size(); i++) {
            x = qss.get(i);
            for (int j = 0; j < pss.size(); j++) {
                if (x != pss.get(j)) {
                    y = pss.get(j);
                    f = true;
                    break;
                }
            }
            if (f)
                break;

        }
        if (f && ans > n - q.get(x) - p.get(y))
            ans = n - q.get(x) - p.get(y);
        System.out.println(ans);
    }
}

Sorting * Java * hashes is very difficult to implement. If you usually do * Java *, do you sort by class?

Ruby Perl Java
Code length 694 Byte 808 Byte 2646 Byte
Execution time 164 ms 131 ms 649 ms
memory 24808 KB 28616 KB 54320 KB

Summary

Referenced site

Recommended Posts

Sorting AtCoder ABC 111 C hashes to solve in Ruby, Perl and Java
AtCoder ARC 081 C hash to solve in Ruby, Perl and Java
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)
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 2) Dynamic programming
AtCoder dwango Programming Contest B in Ruby, Perl and Java
Java to C and C to Java in Android Studio
Solving with Ruby, Perl and Java AtCoder ABC 136 D Breadth-first search
AtCoder ABC 169 C Floating Point Fits in Ruby
AtCoder ABC127 D hash to solve with Ruby 2.7.1
Solving with Ruby and Java AtCoder ABC129 D 2D array
Differences in how to handle strings between Java and Perl
Solve AtCoder Beginner Contest 151 in java
Sorting hashes in a Ruby array
Solve AtCoder Beginner Contest 153 in java
Solve AtCoder Beginner Contest 175 in java
Solve AtCoder Beginner Contest 160 in java
Solve AtCoder Beginner Contest 152 in java
Solve AtCoder Beginner Contest 156 in java
AtCoder ARC 081 C hash to solve in Ruby, Perl and Java
Solving with Ruby, Perl and Java AtCoder ABC 128 C
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 1)
Solving with Ruby, Perl and Java AtCoder ABC 136 D Breadth-first search
Solve with Ruby AtCoder ABC177 D UnionFind
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
AtCoder ABC127 D hash to solve with Ruby 2.7.1
Solving with Ruby and Java AtCoder ABC129 D 2D array
[At Coder] Solve the ABC183 D problem with Ruby
[At Coder] Solve the ABC182 D problem with Ruby
Solving in Ruby, Perl and Java AtCoder ABC 113 C Reference
Solve Google problems with Ruby
Solve with Ruby AtCoder ABC177 D UnionFind
Try to solve Project Euler in Java
Java implementation to create and solve mazes
Summary of hashes and symbols in Ruby
Java Direction in C ++ Design and Evolution
[Ruby / Refactoring] From Ruby iterative processing like Java and C language to Ruby-like iterative processing
Try to link Ruby and Java with Dapr
Differences in writing Java, C # and Javascript classes
Solving with Ruby and Crystal AtCoder ABC 129 D
How to solve an Expression Problem in Java
Organize builds in C ++ / Java and Win / Linux combinations
AtCoder Beginner Contest 170 A, B, C up to ruby
Java classes and instances to understand in the figure
How to convert A to a and a to A using AND and OR in Java
How to handle TSV files and CSV files in Ruby
Try to solve a restricted FizzBuzz problem in Java
Gzip-compress byte array in Java and output to file
atcoder ABC113 C problem
From Java to Ruby !!
atcoder ABC115 C problem
Math Girls Secret Note 104th implemented in Ruby and C
[Java] Change language and locale to English in JVM options
Refer to C ++ in the Android Studio module (Java / kotlin)
What happened in "Java 8 to Java 11" and how to build an environment
How to call and use API in Java (Spring Boot)
Reasons to use Servlet and JSP separately in Java development
How to develop and register a Sota app in Java
Write DiscordBot to Spreadsheets Write in Ruby and run with Docker
ABC177 --solving E in Ruby
About Ruby hashes and symbols
Ruby C extension and volatile
Reproduce Java enum in C #
C # and Java Overrides Story
Offline real-time how to write F03 ruby and C implementation example
How to get and add data from Firebase Firestore in Ruby
[Android] Convert Map to JSON using GSON in Kotlin and Java
How to solve the unknown error when using slf4j in Java
Sample to read and write LibreOffice Calc fods file in Java 2021
Things to remember and concepts in the Ruby on Rails tutorial
How to encrypt and decrypt with RSA public key in Java