結果

問題 No.198 キャンディー・ボックス2
ユーザー kohei2019kohei2019
提出日時 2021-09-09 21:34:33
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,656 bytes
コンパイル時間 2,251 ms
コンパイル使用メモリ 137,224 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-30 02:42:52
合計ジャッジ時間 3,690 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 WA -
testcase_24 AC 2 ms
4,380 KB
testcase_25 WA -
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 WA -
testcase_29 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:76:21: warning: field 'N' is uninitialized when used here [-Wuninitialized]
    int group_cnt = N;
                    ^
main.cpp:77:5: note: during field initialization in this constructor
    UnionFind(int N){
    ^
1 warning generated.

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using PAIR = pair<int, int>;
using PAIRLL = pair<ll,ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vs = vector<string>;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define INF 100000000
#define REP(i,n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)
#define FORA(i,I) for(const auto& i:I)
//x:コンテナ
#define ALL(x) x.begin(),x.end() 
#define SIZE(x) ll(x.size()) 
//定数
#define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf
#define INF64 9223372036854775807 //9.223372036854775807×10^{18}:64bit整数のinf
#define MOD 1000000007 //問題による
#define F first
#define S second
//出力(空白区切りで昇順に)
#define coutALL(x) for(auto i=x.begin();i!=--x.end();i++)cout<<*i<<" ";cout<<*--x.end()<<endl
#define coutALLn(x) for(auto i=x.begin();i!=--x.end();i++)cout<<*i<<endl;cout<<*--x.end()<<endl
ll myceil(ll a,ll b){return (a+(b-1))/b;}
ll myfloor(ll a,ll b){return a/b;}

int vector_finder(std::vector<int> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance( vec.begin(), itr );
  if (index != vec.size()) { // 発見できたとき
    return 1;
  }
  else { // 発見できなかったとき
    return 0;
  }
}

vector<ll> vector_find_ll(vector<ll> vec,ll num){
    int N = vec.size();
    vector<ll> match_num;
    rep(i,N){
        if (vec[i] == num){
            match_num.push_back(i);
        }
    }
    return match_num;
}

ll vector_max(vector<ll> vec){
    ll ret = -INF64;
    int N = vec.size();
    rep(i,N){
        ret = max(ret,vec[i]);
    }
    return ret;
}

ll vector_min(vector<ll> vec){
    ll ret = INF64;
    int N = vec.size();
    rep(i,N){
        ret = min(ret,vec[i]);
    }
    return ret;
}

struct UnionFind{
    int N;
    vector<int> parents;
    vector<int> sizeg;
    int group_cnt = N;
    UnionFind(int N){
        rep(i,N) parents.push_back(i);
        rep(i,N) sizeg.push_back(1);
    } 
    int find(int x){
        if (x == parents[x]){
            return x;
        }
        else{
            parents[x] = find(parents[x]);
            return parents[x];
        }
    }
    void unite(int x,int y){
        x = find(x);
        y = find(y);
        if (x > y){
            swap(x,y);
        }
        if (x == y){
            return;
        }
        sizeg[x] += sizeg[y];
        parents[y] = x;
        group_cnt -= 1;
    }
    int size(int x){
        return sizeg[find(x)];
    }
    bool same(int x,int y){
        return find(x) == find(y);
    }
    int groupcount(){
        return group_cnt;
    }
};

struct SegTree{
    ll DEFAULT = 0;
    ll func(ll x, ll y){
        return x ^ y;
    }
    int N,K = 0,N2;
    vector<ll> dat;
    SegTree(vector<ll> ls){
        N = ls.size();
        while ((N-1) >> K){
            K += 1;
        }
        //K -= 1;
        N2 = 1 << K;
        rep(i,1<<(K+1)){
           dat.push_back(DEFAULT); 
        }
        rep(i,N){
            dat[N2+i] = ls[i];
        }
        for (int i=N2-1;i>0;i--){
            dat[i] = func(dat[i<<1],dat[i<<1|1]);
        }
    }
    ll leafval(int x){
        return dat[x+N2];
    }
    void update(int x,ll y){
        int i = x + N2;
        dat[i] = y;
        while (i > 0){
            i >>= 1;
            dat[i] = func(dat[i<<1],dat[i<<1|1]);
        }
    }
    ll query(int L,int R){
        L += N2;
        R += N2;
        ll vL = DEFAULT;
        ll vR = DEFAULT;
        while (L < R){
            if (L & 1){
                vL = func(vL,dat[L]);
                L += 1;
            }
            if (R & 1){
                R -= 1;
                vR = func(dat[R],vR);
            }
            L >>= 1;
            R >>= 1;
        }
        return func(vL,vR);
    }
};

ll sumvec(vector<ll> ls){
    ll v = 0;
    int N = ls.size();
    rep(i,N){
        v += ls[i];
    }
    return v;
}

int main(){
    ll B;
    int N;
    cin >> B >> N;
    vector<ll> ls(N);
    rep(i,N) cin >> ls[i];
    sort(ls.begin(),ls.end());
    ll ans = INF64;
    ll sumls = sumvec(ls);
    rep(i,N){
        ll mid = ls[i];
        ll v = 0;
        if (sumls + B >= N * mid){
            rep(i,N){
            v += abs(ls[i]-mid);
            }
            ans = min(ans,v);
        } 


        mid = ls[i]-1;
        v = 0;
        if (sumls + B < N * mid){
            continue;
        } 
        rep(i,N){
            v += abs(ls[i]-mid);
        }
        ans = min(ans,v);
    }
    cout << ans << endl;
}
0