結果

問題 No.198 キャンディー・ボックス2
ユーザー ry0u_ydry0u_yd
提出日時 2015-09-05 18:30:52
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,113 bytes
コンパイル時間 684 ms
コンパイル使用メモリ 68,440 KB
実行使用メモリ 11,852 KB
最終ジャッジ日時 2023-09-26 08:51:22
合計ジャッジ時間 4,939 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int main() {
    int b,n;
    cin >> b >> n;

    vector<int> v(n);
    rep(i,n) cin >> v[i];

    ll left = 0, right = 1000000000;
    ll ans = INF;
    while(right - left > 1) {
        ll l = (left * 2 + right) / 3;
        ll r = (left + right * 2) / 3;

        ll cnt = 0, q = b;
        rep(i,n) {
            cnt += abs(v[i]-l);
            q += v[i] - l;
        }
        if(q < 0) cnt = INF;

        ll cnt2 = 0, q2 = b;
        rep(i,n) {
            cnt2 += abs(v[i]-r);
            q2 += v[i] - r;
        }
        if(q2 < 0) cnt = INF;

        if(cnt <= cnt2) {
            right = r;
        } else {
            left = l;
        }

        ans = min(ans,cnt);
        ans = min(ans,cnt2);
    }

    cout << ans << endl;

    return 0;
}
0