結果
問題 | No.198 キャンディー・ボックス2 |
ユーザー | uafr_cs |
提出日時 | 2015-09-03 00:07:53 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 126 ms / 1,000 ms |
コード長 | 2,906 bytes |
コンパイル時間 | 2,896 ms |
コンパイル使用メモリ | 83,172 KB |
実行使用メモリ | 56,444 KB |
最終ジャッジ日時 | 2023-08-10 07:37:13 |
合計ジャッジ時間 | 8,094 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 119 ms
56,268 KB |
testcase_01 | AC | 122 ms
56,012 KB |
testcase_02 | AC | 121 ms
53,860 KB |
testcase_03 | AC | 120 ms
56,020 KB |
testcase_04 | AC | 123 ms
56,064 KB |
testcase_05 | AC | 120 ms
55,832 KB |
testcase_06 | AC | 120 ms
56,444 KB |
testcase_07 | AC | 125 ms
56,000 KB |
testcase_08 | AC | 123 ms
56,260 KB |
testcase_09 | AC | 122 ms
55,640 KB |
testcase_10 | AC | 122 ms
56,212 KB |
testcase_11 | AC | 124 ms
55,896 KB |
testcase_12 | AC | 121 ms
56,020 KB |
testcase_13 | AC | 120 ms
56,000 KB |
testcase_14 | AC | 121 ms
56,068 KB |
testcase_15 | AC | 121 ms
56,204 KB |
testcase_16 | AC | 125 ms
56,092 KB |
testcase_17 | AC | 124 ms
56,028 KB |
testcase_18 | AC | 126 ms
55,872 KB |
testcase_19 | AC | 123 ms
55,744 KB |
testcase_20 | AC | 124 ms
56,020 KB |
testcase_21 | AC | 124 ms
56,088 KB |
testcase_22 | AC | 122 ms
55,852 KB |
testcase_23 | AC | 121 ms
55,852 KB |
testcase_24 | AC | 123 ms
56,224 KB |
testcase_25 | AC | 122 ms
56,236 KB |
testcase_26 | AC | 124 ms
56,008 KB |
testcase_27 | AC | 122 ms
56,288 KB |
testcase_28 | AC | 124 ms
56,208 KB |
testcase_29 | AC | 123 ms
56,184 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); } }