結果
問題 | No.198 キャンディー・ボックス2 |
ユーザー | uafr_cs |
提出日時 | 2015-09-03 00:07:53 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 136 ms / 1,000 ms |
コード長 | 2,906 bytes |
コンパイル時間 | 2,684 ms |
コンパイル使用メモリ | 83,480 KB |
実行使用メモリ | 41,664 KB |
最終ジャッジ日時 | 2024-11-16 09:07:39 |
合計ジャッジ時間 | 7,596 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 133 ms
41,504 KB |
testcase_01 | AC | 131 ms
41,360 KB |
testcase_02 | AC | 136 ms
41,136 KB |
testcase_03 | AC | 133 ms
41,372 KB |
testcase_04 | AC | 134 ms
41,524 KB |
testcase_05 | AC | 132 ms
41,480 KB |
testcase_06 | AC | 119 ms
40,500 KB |
testcase_07 | AC | 131 ms
41,304 KB |
testcase_08 | AC | 129 ms
41,388 KB |
testcase_09 | AC | 129 ms
41,612 KB |
testcase_10 | AC | 130 ms
41,124 KB |
testcase_11 | AC | 128 ms
41,164 KB |
testcase_12 | AC | 131 ms
41,352 KB |
testcase_13 | AC | 131 ms
41,444 KB |
testcase_14 | AC | 118 ms
40,508 KB |
testcase_15 | AC | 129 ms
41,448 KB |
testcase_16 | AC | 133 ms
41,416 KB |
testcase_17 | AC | 131 ms
41,076 KB |
testcase_18 | AC | 127 ms
40,992 KB |
testcase_19 | AC | 133 ms
41,484 KB |
testcase_20 | AC | 133 ms
41,664 KB |
testcase_21 | AC | 132 ms
41,524 KB |
testcase_22 | AC | 132 ms
41,352 KB |
testcase_23 | AC | 120 ms
40,228 KB |
testcase_24 | AC | 119 ms
40,208 KB |
testcase_25 | AC | 119 ms
40,200 KB |
testcase_26 | AC | 134 ms
41,240 KB |
testcase_27 | AC | 132 ms
41,364 KB |
testcase_28 | AC | 131 ms
41,504 KB |
testcase_29 | AC | 133 ms
41,496 KB |
ソースコード
import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.Random; import java.util.Scanner; import java.util.Set; public class Main { public static long solve_smart(long B, int N, long[] arr){ long score = Long.MAX_VALUE; for(int pos = 0; pos < N; pos++){ long benefit = B, current_score = 0; for(int i = 0; i < N; i++){ benefit += (arr[i] - arr[pos]); current_score += Math.abs(arr[i] - arr[pos]); } if(benefit < 0){ continue; } score = Math.min(score, current_score); } { final long max = (Arrays.stream(arr).sum() + B) / N; System.out.println(max); long current_score = 0; for(int i = 0; i < N; i++){ current_score += Math.abs(arr[i] - max); } score = Math.min(score , current_score); } return score; } public static long solve_naive(long B, int N, long[] arr){ //System.out.println(Arrays.toString(arr)); final long max = Arrays.stream(arr).max().getAsLong(); long score = Long.MAX_VALUE, min_cur = -1; for(long cur = 0; cur <= max; cur++){ long benefit = B, current_score = 0; for(int i = 0; i < N; i++){ benefit += (arr[i] - cur); current_score += Math.abs(arr[i] - cur); } if(benefit < 0){ continue; } if(score > current_score){ score = current_score; min_cur = cur; } score = Math.min(score, current_score); } System.out.println(B + " " + N + " " + Arrays.toString(arr) + " " + min_cur + " " + Arrays.stream(arr).average() + " " + score); return score; } public static void check(){ Random rnd = new Random(); while(true){ final long B = rnd.nextInt(1000); final int N = rnd.nextInt(9) + 1; long[] arr = new long[N]; for(int i = 0; i < N; i++){ arr[i] = rnd.nextInt(100); } final long smart = solve_smart(B, N, arr); final long naive = solve_naive(B, N, arr); if(smart != naive){ System.out.println(">>>> naive = " + naive + ", smart = " + smart); // } } } public static void main(String[] args) { //check(); Scanner sc = new Scanner(System.in); final long B = sc.nextLong(); final int N = sc.nextInt(); long[] arr = new long[N]; for(int i = 0; i < N; i++){ final long c = sc.nextLong(); arr[i] = c; } Arrays.sort(arr); long score = Long.MAX_VALUE; for(int pos = 0; pos < N; pos++){ long benefit = B, current_score = 0; for(int i = 0; i < N; i++){ benefit += (arr[i] - arr[pos]); current_score += Math.abs(arr[i] - arr[pos]); } if(benefit < 0){ continue; } score = Math.min(score, current_score); } { final long max = (Arrays.stream(arr).sum() + B) / N; long current_score = 0; for(int i = 0; i < N; i++){ current_score += Math.abs(arr[i] - max); } score = Math.min(score , current_score); } System.out.println(score); } }