結果
問題 | No.198 キャンディー・ボックス2 |
ユーザー |
![]() |
提出日時 | 2020-01-23 13:22:18 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,141 bytes |
コンパイル時間 | 2,203 ms |
コンパイル使用メモリ | 76,848 KB |
実行使用メモリ | 41,516 KB |
最終ジャッジ日時 | 2024-07-19 04:55:25 |
合計ジャッジ時間 | 6,254 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 14 WA * 12 |
ソースコード
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; } }