結果

問題 No.198 キャンディー・ボックス2
ユーザー htensaihtensai
提出日時 2020-01-23 13:22:18
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,141 bytes
コンパイル時間 2,283 ms
コンパイル使用メモリ 74,360 KB
実行使用メモリ 56,440 KB
最終ジャッジ日時 2023-09-26 10:03:01
合計ジャッジ時間 7,302 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 123 ms
55,816 KB
testcase_03 AC 124 ms
56,076 KB
testcase_04 AC 122 ms
55,924 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 123 ms
56,180 KB
testcase_08 WA -
testcase_09 AC 122 ms
56,084 KB
testcase_10 AC 124 ms
56,096 KB
testcase_11 AC 121 ms
55,764 KB
testcase_12 AC 122 ms
56,104 KB
testcase_13 WA -
testcase_14 AC 118 ms
55,940 KB
testcase_15 AC 120 ms
55,968 KB
testcase_16 AC 120 ms
55,968 KB
testcase_17 AC 120 ms
55,520 KB
testcase_18 AC 122 ms
55,712 KB
testcase_19 AC 124 ms
56,440 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 122 ms
55,548 KB
testcase_25 AC 123 ms
55,952 KB
testcase_26 AC 124 ms
55,812 KB
testcase_27 AC 119 ms
55,816 KB
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[]  args) {
        Scanner sc = new Scanner(System.in);
        long total = sc.nextInt();
        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
            total += arr[i];
        }
        long left = 0;
        long right = total / n;
        boolean isRight = false;
        while (right - left > 2) {
            long m1 = (left * 2 + right) / 3;
            long m2 = (left + right * 2) / 3;
            if (calc((int)m1, arr) <= calc((int)m2, arr)) {
                right = m2;
                isRight = false;
            } else {
                isRight = true;
                left = m1;
            }
        }
        System.out.println(Math.min(Math.min(calc((int)right, arr), calc((int)left, arr)), Math.min(calc((int)((left * 2 + right) / 3), arr), calc((int)((left + right * 2) / 3), arr))));
    }
    
    static int calc(int x, int[] arr) {
        int ans = 0;
        for (int y : arr) {
            ans += Math.abs(x - y);
        }
        return ans;
    }
}
0