結果
問題 | No.198 キャンディー・ボックス2 |
ユーザー |
|
提出日時 | 2022-02-24 12:22:48 |
言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 1,000 ms |
コード長 | 1,345 bytes |
コンパイル時間 | 2,083 ms |
コンパイル使用メモリ | 144,728 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-02 11:23:25 |
合計ジャッジ時間 | 2,187 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
#include <iostream> #include <utility> #include <vector> #include <string> #include <cmath> #include <algorithm> #include <functional> #include <iomanip> #include <map> #include <set> #include <queue> #include <complex> #include <cassert> using namespace std; using ll = long long int; #define CHMAX(x,y) (x) = ((x) > (y)) ? (x) : ((x) = (y)); #define CHMIN(x,y) (x) = ((x) < (y)) ? (x) : ((x) = (y)); #define REP(i,n) for(int (i)=0;(i)<(n);++(i)) #define RREP(i,n,N) for(int (i)=(n);(i)<(N);++(i)) #define DREP(i,n) for(int (i)=(n);(i)>=0;--(i)) #define DRREP(i,n,N) for(int i=(n);(i)>=(N);--(i)) const int INF = 1e9; const ll INFL = 1e18; const int Mod = 1e9 + 7; const int dx[] = {0,1,0,-1}; const int dy[] = {1,0,-1,0}; int main(int argc, char const *argv[]) { ll B,N; cin >> B >> N; vector<ll> C(N); ll sum = B; REP(i,N)cin >> C[i]; REP(i,N)sum+=C[i]; if(N == 1) { std::cout << 0 << std::endl; return 0; } auto f = [&](ll K)->ll { ll res = 0; REP(i,N)res += abs(C[i]-K); return res; }; sort(C.begin(),C.end()); ll low = C[0]; ll high = (sum/N) + 1; while(high - low > 3) { ll c1 = (2*low + high)/3; ll c2 = (low + 2*high)/3; if(f(c1) < f(c2))high = c2; else low = c1; } ll answer = INFL; RREP(i,low,high)CHMIN(answer,f(i)); std::cout << answer << std::endl; }