# Introduction

• AtCoder Problems * Use Recommendations to solve past problems. Thank you, AtCoder Problems.

# 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