結果

問題 No.198 キャンディー・ボックス2
ユーザー tsunabittsunabit
提出日時 2020-03-14 20:38:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 126 ms / 1,000 ms
コード長 851 bytes
コンパイル時間 3,879 ms
コンパイル使用メモリ 74,068 KB
実行使用メモリ 58,172 KB
最終ジャッジ日時 2023-08-15 14:00:53
合計ジャッジ時間 9,057 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
55,808 KB
testcase_01 AC 119 ms
56,124 KB
testcase_02 AC 119 ms
56,468 KB
testcase_03 AC 115 ms
56,144 KB
testcase_04 AC 118 ms
56,184 KB
testcase_05 AC 117 ms
56,184 KB
testcase_06 AC 120 ms
56,188 KB
testcase_07 AC 117 ms
55,928 KB
testcase_08 AC 125 ms
55,652 KB
testcase_09 AC 120 ms
55,768 KB
testcase_10 AC 119 ms
55,832 KB
testcase_11 AC 116 ms
56,200 KB
testcase_12 AC 118 ms
55,964 KB
testcase_13 AC 120 ms
58,172 KB
testcase_14 AC 118 ms
55,560 KB
testcase_15 AC 122 ms
55,492 KB
testcase_16 AC 124 ms
55,812 KB
testcase_17 AC 126 ms
56,292 KB
testcase_18 AC 121 ms
56,036 KB
testcase_19 AC 123 ms
55,536 KB
testcase_20 AC 123 ms
55,540 KB
testcase_21 AC 125 ms
54,312 KB
testcase_22 AC 121 ms
56,196 KB
testcase_23 AC 120 ms
55,996 KB
testcase_24 AC 119 ms
53,704 KB
testcase_25 AC 121 ms
55,932 KB
testcase_26 AC 119 ms
55,888 KB
testcase_27 AC 119 ms
56,112 KB
testcase_28 AC 120 ms
55,984 KB
testcase_29 AC 121 ms
56,192 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