結果

問題 No.198 キャンディー・ボックス2
ユーザー inu_hir0shiinu_hir0shi
提出日時 2015-04-29 00:53:09
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,681 bytes
コンパイル時間 2,103 ms
コンパイル使用メモリ 150,364 KB
実行使用メモリ 5,316 KB
最終ジャッジ日時 2023-09-19 03:26:27
合計ジャッジ時間 3,555 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
typedef long long LL;
typedef unsigned long long ULL;
template<class T> inline bool amax (T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool amin (T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> ostream& operator << (ostream &os, const vector<T> &v) { os << "["; for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); it++) { os << (it != v.begin() ? ", " : "") << *it; } os << "]"; return os; }
template<class T> ostream& operator << (ostream &os, const set<T> &s) { os << "["; for (typename set<T>::const_iterator it = s.begin(); it != s.end(); it++) { os << (it != s.begin() ? ", " : "") << *it; } os << "]"; return os; }
template<class Key, class Val> ostream& operator << (ostream &os, const map<Key, Val> &m) { os << "{"; for (typename map<Key, Val>::const_iterator it = m.begin(); it != m.end(); it++) { os << (it != m.begin() ? ", " : "") << it->first << ":" << it->second; } os << "}"; return os; }
template<class T, class S> ostream& operator << (ostream &os, const pair<T, S> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <class Target, class Source> inline Target lexical_cast (const Source &s) { Target t; stringstream ss; ss << s; ss >> t; return t; }

//> v < ^ (clock wise)
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
const int INFI = 1<<28;
const long long int INFL = 1LL<<60;
const double INFD = 1e+300;
const float INFF = 1e+100;
const double EPS = 1e-8;

LL f (LL t, LL B, vector<LL> C) {
    LL r = 0;
    for (int i = 0; i < C.size(); i++) {
        if (C[i] > t) B += C[i]-t, r += C[i]-t,C[i] = t;
    }
    for (int i = 0; i < C.size(); i++) {
        B -= t-C[i], r += t-C[i];
    }
    if (B < 0) return INFI;
    return r;
}

int main(){
    cout.setf(ios::fixed); cout.precision(10);
    ios_base::sync_with_stdio(false);
    LL B, N;
    cin >> B >> N;
    vector<LL> C(N);
    for (int i = 0; i < N; i++) cin >> C[i];
    sort(C.begin(), C.end());
    LL cost = 0;
    while (1) {
        LL diff = 0;
        for (int i = 0; i < N-1; i++) {
            if (C[i] != C[i+1]) {
                diff = C[i+1] - C[i];
                break;
            }
        }
        if (diff == 0) {
            cout << cost << endl;
            return 0;
        }
        LL add = min(B, diff);
        C[0] += add;
        B -= add;
        cost += add;
        sort(C.begin(), C.end());
        if (B == 0) break;
    }

    LL sum = 0;
    for (int i = 0; i < N; i++) sum += C[i];
    LL t = sum / N;
    cout << cost + f(t, B, C) << endl;



    return 0;
}
0