結果

問題 No.198 キャンディー・ボックス2
ユーザー tsunabittsunabit
提出日時 2020-03-14 20:38:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 142 ms / 1,000 ms
コード長 851 bytes
コンパイル時間 3,525 ms
コンパイル使用メモリ 78,008 KB
実行使用メモリ 41,420 KB
最終ジャッジ日時 2024-05-03 01:30:07
合計ジャッジ時間 8,783 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
41,348 KB
testcase_01 AC 135 ms
41,316 KB
testcase_02 AC 135 ms
41,120 KB
testcase_03 AC 135 ms
41,420 KB
testcase_04 AC 135 ms
41,336 KB
testcase_05 AC 130 ms
41,056 KB
testcase_06 AC 136 ms
41,392 KB
testcase_07 AC 142 ms
41,320 KB
testcase_08 AC 135 ms
41,052 KB
testcase_09 AC 120 ms
39,896 KB
testcase_10 AC 136 ms
41,212 KB
testcase_11 AC 140 ms
41,208 KB
testcase_12 AC 135 ms
41,368 KB
testcase_13 AC 136 ms
41,300 KB
testcase_14 AC 133 ms
41,168 KB
testcase_15 AC 133 ms
41,260 KB
testcase_16 AC 131 ms
41,044 KB
testcase_17 AC 133 ms
41,128 KB
testcase_18 AC 132 ms
41,312 KB
testcase_19 AC 130 ms
41,128 KB
testcase_20 AC 131 ms
41,340 KB
testcase_21 AC 133 ms
41,332 KB
testcase_22 AC 136 ms
41,304 KB
testcase_23 AC 124 ms
39,820 KB
testcase_24 AC 134 ms
41,252 KB
testcase_25 AC 131 ms
41,200 KB
testcase_26 AC 133 ms
41,176 KB
testcase_27 AC 120 ms
40,148 KB
testcase_28 AC 136 ms
41,260 KB
testcase_29 AC 132 ms
41,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
import java.math.*;

public class No198 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int b = sc.nextInt(), n = sc.nextInt();
		int[] c = new int[n];
		
		if(n == 1) {
			System.out.println(0);
			return;
		}
		
		long sum = b;
		for(int i = 0; i < n; i++) {
			c[i] = sc.nextInt();
			sum += c[i];
		}
		
		long l = 0;
		long r = sum / n;
		while(r - l > 2) {
			long p0 = (l * 2 + r) / 3;
			long p1 = (l + r * 2) / 3;
			
			if(cnt(c, p0) > cnt(c, p1)) {
				l = p0;
			}else {
				r = p1;
			}
		}
		long ans = Long.MAX_VALUE;
		for(long i = l; i <= r; i++) {
			ans = Math.min(ans, cnt(c, i));
		}
		System.out.println(ans);
	}
	public static long cnt(int[] c, long p) {
		long t = 0;
		for(int i = 0; i < c.length; i++) {
			t += Math.abs(p-c[i]);
		}
		return t;
	}
}
0