結果

問題 No.198 キャンディー・ボックス2
ユーザー GrenacheGrenache
提出日時 2016-02-15 20:58:40
言語 Java21
(openjdk 21)
結果
AC  
実行時間 143 ms / 1,000 ms
コード長 1,293 bytes
コンパイル時間 3,475 ms
コンパイル使用メモリ 78,072 KB
実行使用メモリ 54,472 KB
最終ジャッジ日時 2024-04-28 01:49:40
合計ジャッジ時間 8,588 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
54,104 KB
testcase_01 AC 137 ms
54,472 KB
testcase_02 AC 138 ms
54,264 KB
testcase_03 AC 136 ms
53,648 KB
testcase_04 AC 143 ms
54,324 KB
testcase_05 AC 140 ms
53,800 KB
testcase_06 AC 128 ms
53,224 KB
testcase_07 AC 138 ms
53,860 KB
testcase_08 AC 133 ms
54,128 KB
testcase_09 AC 130 ms
53,740 KB
testcase_10 AC 129 ms
54,028 KB
testcase_11 AC 118 ms
52,704 KB
testcase_12 AC 130 ms
53,560 KB
testcase_13 AC 132 ms
54,132 KB
testcase_14 AC 133 ms
54,324 KB
testcase_15 AC 131 ms
53,680 KB
testcase_16 AC 131 ms
53,888 KB
testcase_17 AC 132 ms
54,124 KB
testcase_18 AC 131 ms
54,048 KB
testcase_19 AC 131 ms
53,956 KB
testcase_20 AC 120 ms
52,508 KB
testcase_21 AC 132 ms
54,056 KB
testcase_22 AC 133 ms
53,976 KB
testcase_23 AC 133 ms
54,132 KB
testcase_24 AC 138 ms
54,088 KB
testcase_25 AC 133 ms
54,128 KB
testcase_26 AC 135 ms
54,000 KB
testcase_27 AC 137 ms
53,684 KB
testcase_28 AC 135 ms
54,204 KB
testcase_29 AC 133 ms
53,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;


public class Main_yukicoder198 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int b = sc.nextInt();
        int n = sc.nextInt();
        int[] c = new int[n];
        long sum = 0;
        for (int i = 0; i < n; i++) {
        	c[i] = sc.nextInt();
        	sum += c[i];
        }
        Arrays.sort(c);

        long l = 0;
        long r = 1_000_000_000;
        while (r - l > 5) {
        	long c1 = (l + l + r) / 3;
        	long c2 = (l + r + r) / 3;

        	if (f(c1, b, n, c, sum) > f(c2, b, n, c, sum)) {
        		l = c1;
        	} else {
        		r = c2;
        	}
        }

        long min = Long.MAX_VALUE;
//        long mini = 0;
        for (long i = l; i <= r; i++) {
        	long tmp = f(i, b, n, c, sum);
        	if (tmp < min) {
        		min = tmp;
//        		mini = i;
        	}
        }

        System.out.println(min);
//        System.out.println(mini);

        sc.close();
    }

	private static long f(long x, int b, int n, int[] c, long sum) {
		if (n == 1) {
			return 0;
		}

		if (x * n > sum + b) {
			return Long.MAX_VALUE;
		}

		long ret = 0;
		for (int i = n - 1; i >= 0; i--) {
			ret += Math.abs(c[i] - x);
		}

		return ret;
	}
}
0