AtCoder ARC 081 C hash to solve in Ruby, Perl and Java

Introduction

This theme

AtCoder Regular Contest C - Make a Rectangle Difficulty: 538

This theme, hash Ruby

ruby.rb


n = gets.to_i
c = gets.split.map(&:to_i)
h = {}
n.times do |i|
  if h[c[i]]
    h[c[i]] += 1
  else
    h[c[i]] = 1
  end
end
s = h.select{|k, v| v >= 4}.keys.sort{|a, b| b<=>a}
t = h.select{|k, v| v >= 2}.keys.sort{|a, b| b<=>a}
ans = 0
ans = s[0] * s[0] if s.size > 0
ans = [ans, t[0] * t[1]].max if t.size > 1
puts ans

Use the hash to find out the number of occurrences. In my ring Ruby (2.7.1), the filter method can be used, but in the old environment Ruby (2.3.3) of AtCoder, it becomes ** RE ** </ font>. So I'm using the select method. Perl

perl.pl


use v5.18; # strict say state
use warnings;
use List::Util qw(max);

chomp (my $n = <STDIN>);
chomp (my @a = split / /, <STDIN>);
my %h;
$h{$_}++ for @a;
my @s = sort {$b <=> $a} grep {$h{$_} >= 4} keys %h;
my @t = sort {$b <=> $a} grep {$h{$_} >= 2} keys %h;
my $ans = 0;
$ans = $s[0] * $s[0] if @s > 0;
$ans = max($ans, $t[0] * $t[1]) if @t > 1;
say $ans;

It uses Perl idioms.

hash.pl


$h{$_}++ for @a;

Java

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());
        int a[] = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(sc.next());
        }
        sc.close();
        Map<Integer, Integer> h = new TreeMap<>(Comparator.reverseOrder());
        for (int i = 0; i < n; i++) {
            if (h.containsKey(a[i])) {
                h.put(a[i], h.get(a[i]) + 1);
            } else {
                h.put(a[i], 1);
            }
        }
        List<Integer> s = h.entrySet().stream()
                .filter(x -> x.getValue() >= 4)
                .map(x -> x.getKey())
                .collect(Collectors.toList());
        List<Integer> t = h.entrySet().stream()
                .filter(x -> x.getValue() >= 2)
                .map(x -> x.getKey())
                .collect(Collectors.toList());
        long ans = 0;
        if (s.size() > 0) {
            ans = (long) s.get(0) * s.get(0);
        }
        if (t.size() > 1) {
            ans = Math.max(ans, (long) t.get(0) * t.get(1));
        }
        System.out.println(ans);
    }
}

I'm using a Java lambda expression. TreeMap is a hash, but Comparator.reverseOrder () can automatically sort the Keys in descending order. It should be converted to long as an integer overflow countermeasure.

Ruby Perl Java
Code length 336 Byte 370 Byte 1199 Byte
Execution time 140 ms 135 ms 664 ms
memory 14988 KB 28196 KB 48532 KB

Summary

  • Solved ARC 081 C
  • Become familiar with Ruby
  • Become familiar with Perl
  • Become familiar with Java

Referenced site

Recommended Posts

AtCoder ARC 081 C hash to solve in Ruby, Perl and Java
Sorting AtCoder ABC 111 C hashes 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
AtCoder dwango Programming Contest B in Ruby, Perl and Java
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 1)
Java to C and C to Java in Android Studio
AtCoder ABC127 D hash to solve with Ruby 2.7.1
Solve ARC104 D Multiset Mean with Scala, Java, C ++, Ruby, Perl, Elixir
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 2) Dynamic programming
Differences in how to handle strings between Java and Perl
Solve AtCoder Beginner Contest 151 in java
Solve AtCoder Beginner Contest 150 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
Solving with Ruby, Perl and Java AtCoder ABC 136 D Breadth-first search
Try to solve Project Euler in Java
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
How to solve an Expression Problem in Java
Solving with Ruby AtCoder ACL Beginner Contest C Union Find (DSU)
Solve with Ruby AtCoder ABC177 D UnionFind
AtCoder ABC127 D hash to solve with Ruby 2.7.1
Solving with Ruby and Java AtCoder ABC129 D 2D array
Organize builds in C ++ / Java and Win / Linux combinations
AtCoder Beginner Contest 170 A, B, C up to ruby
From Java 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
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)
How to retrieve the hash value in an array in Ruby
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
Ruby C extension and volatile
Reproduce Java enum in C #
C # and Java Overrides Story
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
I tried to solve the past 10 questions that should be solved after registering with AtCoder in Java
How to iterate infinitely in Ruby
Read Java properties file in C #
Try to implement Yubaba in Ruby
Multithreaded to fit in [Java] template
Encoding and Decoding example in Java
Java, JavaScript, C # (difference in assignment)
How to learn JAVA in 7 days
CGI in C and Dart: Introduction (1)