LoginSignup
1
0

More than 3 years have passed since last update.

Ruby で解けず Java と Crystal で解く AtCoder ABC 189 C

Posted at

はじめに

AtCoder Problems の Recommendation を利用して、過去の問題を解いています。
AtCoder さん、AtCoder Problems さん、ありがとうございます。

今回のお題

AtCoder Beginner Contest C - Mandarin Orange
Difficulty: 565

今回のテーマ、トランスパイラ

コンテスト中にRubyのコンテスタントが7名しか解けなかった問題です。
実行時間制限: 1.5 sec と厳しいのですが、Difficultyは茶色でした。

Java

java.java
import java.util.*;

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();
        long ans = 0;
        long max = 0;
        for (int i = 0; i < n; i++) {
            long min = 1000000000;
            for (int j = i; j < n; j++) {
                if (min > a[j]) {
                    min = a[j];
                }
                if (max < min * (j - i + 1)) {
                    max = min * (j - i + 1);
                }
            }
            if (ans < max) {
                ans = max;
            }
        }
        System.out.println(ans);
    }
}

コンテスト中にRubyからJavaに切り替えて解答しました。
コードとして捻りも何もないのですが、短時間では脳が付いていかないです。

Crystal

Crystal.cr
n = gets.to_s.to_i
a = gets.to_s.split.map { |c| c.to_i }
cnt = 0
max = 0
n.times do |i|
  min = 10**9
  i.upto(n - 1) do |j|
    min = a[j] if min > a[j]
    max = min * (j - i + 1) if max < min * (j - i + 1)
  end
  cnt = max if cnt < max
end
puts cnt

このコードはRubyでもそのまま動作します。
Windows10Crystalが動作すれば代替してもいいのですが。

Ruby Java Crystal
コード長 (Byte) 265 831 265
実行時間 (ms) TLE 268 76
メモリ (KB) 14816 43072 3860

まとめ

  • ABC 189 C を解いた
  • Crystal に詳しくなった
1
0
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0