結果

問題 No.198 キャンディー・ボックス2
ユーザー YamaKasaYamaKasa
提出日時 2018-08-09 20:03:11
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 895 bytes
コンパイル時間 2,449 ms
コンパイル使用メモリ 77,520 KB
実行使用メモリ 57,668 KB
最終ジャッジ日時 2023-10-24 12:35:12
合計ジャッジ時間 6,873 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 125 ms
57,044 KB
testcase_10 AC 126 ms
57,316 KB
testcase_11 AC 123 ms
57,328 KB
testcase_12 AC 129 ms
57,380 KB
testcase_13 WA -
testcase_14 AC 129 ms
57,440 KB
testcase_15 AC 128 ms
57,364 KB
testcase_16 AC 124 ms
55,432 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		long B = scan.nextLong();
		int N = scan.nextInt();
		long[] C = new long[N];
		long sum = B;
		for(int i = 0; i < N; i++) {
			C[i] = scan.nextLong();
			sum += C[i];
		}
		scan.close();
		if(N == 1) {
			System.out.println("0");
			System.exit(0);
		}
		long r = sum / N;
		long l = 0;

		long min = Long.MAX_VALUE;
		while(r - l > 3) {
			long x1 = (l * 2 + r) / 3;
			long x2 = (l + r) / 3;
			long t1 = 0;
			long t2 = 0;
			for(int i = 0; i < N; i++) {
				t1 += Math.abs(x1 - C[i]);
				t2 += Math.abs(x2 - C[i]);
			}
			if(t1 > t2) {
				l = x1;
			}else {
				r = x2;
			}
		}
		for(long i = l; i <= r; i++) {
			long t = 0;
			for(int j = 0; j < N; j++) {
				t += Math.abs(i - C[j]);
			}
			min = Math.min(min, t);
		}
		System.out.println(min);
	}
}
0