結果

問題 No.966 引き算をして門松列(その1)
ユーザー CuriousFairy315CuriousFairy315
提出日時 2020-01-13 20:39:58
言語 Java21
(openjdk 21)
結果
AC  
実行時間 524 ms / 2,000 ms
コード長 1,591 bytes
コンパイル時間 2,527 ms
コンパイル使用メモリ 77,012 KB
実行使用メモリ 59,392 KB
最終ジャッジ日時 2024-06-01 10:45:16
合計ジャッジ時間 4,808 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
54,136 KB
testcase_01 AC 135 ms
54,248 KB
testcase_02 AC 135 ms
54,048 KB
testcase_03 AC 150 ms
54,372 KB
testcase_04 AC 436 ms
59,392 KB
testcase_05 AC 484 ms
59,392 KB
testcase_06 AC 524 ms
59,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        new Main();
    }
    
    public Main() {
        try (Scanner sc = new Scanner(System.in)) {
            int T = sc.nextInt();
            while((T--) > 0) {
                int A = sc.nextInt(), B = sc.nextInt(), C = sc.nextInt();
                int a, b, c;
                int INF = 2_000_000_000;
                int cost = INF;
                if (C > 1 && B > 2) { // 1. A<C<B
                    b = B;
                    c = Math.min(b - 1, C);
                    a = Math.min(c - 1, A);
                    cost = Math.min(cost, (A - a) + (B - b) + (C - c));
                }
                if (A > 1 && B > 2) { // 2. C<A<B
                    b = B;
                    a = Math.min(b - 1, A);
                    c = Math.min(a - 1, C);
                    cost = Math.min(cost, (A - a) + (B - b) + (C - c));
                }
                if (A > 1 && C > 2) { // 3. B<A<C
                    c = C;
                    a = Math.min(c - 1, A);
                    b = Math.min(a - 1, B);
                    cost = Math.min(cost, (A - a) + (B - b) + (C - c));
                }
                if (C > 1 && A > 2) { // 4. B<C<A
                    a = A;
                    c = Math.min(a - 1, C);
                    b = Math.min(c - 1, B);
                    cost = Math.min(cost, (A - a) + (B - b) + (C - c));
                }
                if (cost == INF) System.out.println(-1);
                else System.out.println(cost);
            }
        }
    }
}
0