結果
問題 | No.198 キャンディー・ボックス2 |
ユーザー | uafr_cs |
提出日時 | 2015-09-03 00:07:53 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 119 ms / 1,000 ms |
コード長 | 2,906 bytes |
コンパイル時間 | 2,939 ms |
コンパイル使用メモリ | 84,584 KB |
実行使用メモリ | 41,684 KB |
最終ジャッジ日時 | 2024-04-28 01:12:38 |
合計ジャッジ時間 | 6,339 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 108 ms
41,340 KB |
testcase_01 | AC | 101 ms
40,592 KB |
testcase_02 | AC | 104 ms
40,064 KB |
testcase_03 | AC | 101 ms
40,280 KB |
testcase_04 | AC | 109 ms
41,516 KB |
testcase_05 | AC | 100 ms
40,576 KB |
testcase_06 | AC | 100 ms
40,176 KB |
testcase_07 | AC | 109 ms
41,276 KB |
testcase_08 | AC | 114 ms
41,488 KB |
testcase_09 | AC | 100 ms
40,400 KB |
testcase_10 | AC | 111 ms
40,932 KB |
testcase_11 | AC | 111 ms
41,160 KB |
testcase_12 | AC | 112 ms
41,328 KB |
testcase_13 | AC | 110 ms
41,172 KB |
testcase_14 | AC | 113 ms
41,228 KB |
testcase_15 | AC | 103 ms
40,364 KB |
testcase_16 | AC | 111 ms
41,168 KB |
testcase_17 | AC | 109 ms
41,488 KB |
testcase_18 | AC | 94 ms
39,756 KB |
testcase_19 | AC | 110 ms
41,072 KB |
testcase_20 | AC | 108 ms
41,204 KB |
testcase_21 | AC | 105 ms
41,296 KB |
testcase_22 | AC | 109 ms
41,428 KB |
testcase_23 | AC | 107 ms
41,328 KB |
testcase_24 | AC | 108 ms
41,104 KB |
testcase_25 | AC | 111 ms
40,596 KB |
testcase_26 | AC | 113 ms
41,400 KB |
testcase_27 | AC | 119 ms
41,684 KB |
testcase_28 | AC | 110 ms
40,952 KB |
testcase_29 | AC | 110 ms
41,520 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); } }