結果
問題 | No.198 キャンディー・ボックス2 |
ユーザー |
![]() |
提出日時 | 2016-05-24 13:28:44 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 1,000 ms |
コード長 | 719 bytes |
コンパイル時間 | 686 ms |
コンパイル使用メモリ | 61,024 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-11-16 10:00:29 |
合計ジャッジ時間 | 1,524 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
#include <iostream> #include <cmath> #include <algorithm> #define rep(i,a,b) for(int i=(a);i<(b);i++) #define ll long long using namespace std; int b, n, *c; ll solve(ll k) { ll res = 0, rest = b; rep(i,0,n) { rest += c[i] - k; res += abs(c[i] - k); } return res; } int main(void) { cin >> b >> n; c = new int[n]; ll sum = b; rep(i, 0, n) { cin >> c[i]; sum += c[i]; } ll A, B, C1, C2, T1, T2, res, tmp; A = 0; B = sum / n; while(A < B-10) { C1 = (A+A+B) / 3; C2 = (A+B+B) / 3; T1 = solve(C1); T2 = solve(C2); if(T1 > T2) A = C1; else B = C2; } res = 1000000000000000LL; for(ll i=A;i<=B;i++) { tmp = solve(i); res = min(res, tmp); } cout << res << endl; return 0; }