結果

問題 No.1495 パンの仕入れ
ユーザー penguinmanpenguinman
提出日時 2021-03-31 05:15:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 332 ms / 2,000 ms
コード長 2,295 bytes
コンパイル時間 2,362 ms
コンパイル使用メモリ 206,600 KB
実行使用メモリ 13,696 KB
最終ジャッジ日時 2024-05-09 15:29:36
合計ジャッジ時間 11,574 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 239 ms
5,376 KB
testcase_03 AC 237 ms
5,376 KB
testcase_04 AC 238 ms
5,376 KB
testcase_05 AC 236 ms
5,376 KB
testcase_06 AC 236 ms
5,376 KB
testcase_07 AC 239 ms
5,376 KB
testcase_08 AC 238 ms
5,376 KB
testcase_09 AC 240 ms
5,376 KB
testcase_10 AC 239 ms
5,376 KB
testcase_11 AC 238 ms
5,376 KB
testcase_12 AC 122 ms
5,376 KB
testcase_13 AC 123 ms
5,376 KB
testcase_14 AC 122 ms
5,376 KB
testcase_15 AC 123 ms
5,376 KB
testcase_16 AC 123 ms
5,376 KB
testcase_17 AC 124 ms
5,376 KB
testcase_18 AC 124 ms
5,376 KB
testcase_19 AC 122 ms
5,376 KB
testcase_20 AC 123 ms
5,376 KB
testcase_21 AC 121 ms
5,376 KB
testcase_22 AC 330 ms
13,696 KB
testcase_23 AC 332 ms
13,696 KB
testcase_24 AC 100 ms
5,376 KB
testcase_25 AC 101 ms
5,376 KB
testcase_26 AC 101 ms
5,456 KB
testcase_27 AC 100 ms
5,456 KB
testcase_28 AC 323 ms
13,696 KB
testcase_29 AC 325 ms
13,696 KB
testcase_30 AC 86 ms
5,376 KB
testcase_31 AC 84 ms
5,376 KB
testcase_32 AC 85 ms
5,376 KB
testcase_33 AC 84 ms
5,376 KB
testcase_34 AC 85 ms
5,376 KB
testcase_35 AC 122 ms
5,376 KB
testcase_36 AC 121 ms
5,376 KB
testcase_37 AC 124 ms
5,376 KB
testcase_38 AC 122 ms
5,376 KB
testcase_39 AC 122 ms
5,376 KB
testcase_40 AC 82 ms
5,376 KB
testcase_41 AC 82 ms
5,376 KB
testcase_42 AC 82 ms
5,376 KB
testcase_43 AC 81 ms
5,376 KB
testcase_44 AC 81 ms
5,376 KB
testcase_45 AC 2 ms
5,376 KB
testcase_46 AC 3 ms
5,376 KB
testcase_47 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using ll = long long;
using std::vector;
#define rep(i,j,k) for(int i=int(j); i<int(k); i++)
#define REP(i,j,k) for(int i=int(j); i<=int(k); i++)
const ll inf = (1ll<<60);

int main(){
    ll t; cin >> t;
    assert(1<=t && t<=100);
    ll Nsum = 0, Msum = 0;
    ll Nmax = 2e5, Mmax = 2e5;
    ll Kmax = 1e9;
    ll INF = 1e18;
    while(t--){
        ll N, M, K; cin >> N >> M >> K;
        Nsum += N;
        Msum += M;
        assert(1<=N && N<=Nmax);
        assert(1<=M && M<=Mmax);
        assert(1<=K && K<=Kmax);
        ll max__ = K;
        vector<vector<ll>> data(N);
        rep(i,0,M){
            ll index, val; cin >> index >> val;
            assert(1<=index && index<=N);
            assert(1<=val && val<=Kmax);
            max__ = std::max(max__, val);
            data[index-1].push_back(val);
        }
        assert(max__*max__ <= INF/M);
        //(y-x)^2-(y-(x-1))^2 = 2x-2y-1
        vector<ll> sum(N);
        rep(i,0,N){
            for(auto p:data[i]){
                sum[i] += -p*2-1;
            }
        }
        ll left = -inf, right = inf;
        while(left+1 < right){
            ll mid = (left+right)/2;
            ll cnt = 0;
            rep(i,0,N){
                ll a = 2*data[i].size();
                //ax+sum[i] ≤ mid
                //ax ≤ mid-sum[i]
                //x ≤ (mid-sum[i])/a
                if(0 < a){
                    ll x = std::max(0ll, (mid-sum[i])/a);
                    cnt += x;
                }
                else if(0 <= mid) cnt += inf;
                if(K <= cnt) break;
            }
            if(K <= cnt) right = mid;
            else left = mid;
        }
        ll ans = 0, cnt = 0;
        bool flag = false;
        rep(i,0,N){
            if(data[i].empty()){
                flag = true;
                continue;
            }
            ll a = 2*data[i].size();
            ll x = std::max(0ll, (left-sum[i])/a);
            ans += x*(x+1)/2*a+sum[i]*x;
            cnt += x;
        }
        if(flag && 0<=right) cnt = K;
        ans += (K-cnt)*right;
        rep(i,0,N){
            for(auto p:data[i]) ans += p*p;
        }
        cout << ans << endl;
    }
    assert(Nsum <= Nmax);
    assert(Msum <= Mmax);
}
0