結果

問題 No.1495 パンの仕入れ
ユーザー penguinmanpenguinman
提出日時 2021-03-31 05:08:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 318 ms / 2,000 ms
コード長 1,797 bytes
コンパイル時間 2,605 ms
コンパイル使用メモリ 203,312 KB
実行使用メモリ 13,632 KB
最終ジャッジ日時 2023-09-23 03:29:32
合計ジャッジ時間 16,068 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 233 ms
4,376 KB
testcase_03 AC 226 ms
4,380 KB
testcase_04 AC 231 ms
4,380 KB
testcase_05 AC 225 ms
4,380 KB
testcase_06 AC 224 ms
4,376 KB
testcase_07 AC 227 ms
4,376 KB
testcase_08 AC 227 ms
4,376 KB
testcase_09 AC 227 ms
4,380 KB
testcase_10 AC 227 ms
4,376 KB
testcase_11 AC 230 ms
4,380 KB
testcase_12 AC 117 ms
4,380 KB
testcase_13 AC 116 ms
4,376 KB
testcase_14 AC 116 ms
4,380 KB
testcase_15 AC 115 ms
4,380 KB
testcase_16 AC 116 ms
4,376 KB
testcase_17 AC 116 ms
4,376 KB
testcase_18 AC 116 ms
4,376 KB
testcase_19 AC 116 ms
4,376 KB
testcase_20 AC 116 ms
4,376 KB
testcase_21 AC 116 ms
4,380 KB
testcase_22 AC 318 ms
13,632 KB
testcase_23 AC 315 ms
13,576 KB
testcase_24 AC 95 ms
5,004 KB
testcase_25 AC 94 ms
4,708 KB
testcase_26 AC 93 ms
5,036 KB
testcase_27 AC 94 ms
5,024 KB
testcase_28 AC 311 ms
13,532 KB
testcase_29 AC 301 ms
13,444 KB
testcase_30 AC 80 ms
4,380 KB
testcase_31 AC 80 ms
4,376 KB
testcase_32 AC 81 ms
4,376 KB
testcase_33 AC 80 ms
4,376 KB
testcase_34 AC 81 ms
4,380 KB
testcase_35 AC 116 ms
4,376 KB
testcase_36 AC 115 ms
4,376 KB
testcase_37 AC 116 ms
4,380 KB
testcase_38 AC 116 ms
4,380 KB
testcase_39 AC 115 ms
4,376 KB
testcase_40 AC 79 ms
4,380 KB
testcase_41 AC 77 ms
4,376 KB
testcase_42 AC 77 ms
4,376 KB
testcase_43 AC 78 ms
4,376 KB
testcase_44 AC 77 ms
4,376 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 3 ms
4,380 KB
testcase_47 AC 1 ms
4,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;
    while(t--){
        ll N, M, K; cin >> N >> M >> K;
        vector<vector<ll>> data(N);
        rep(i,0,M){
            ll index, val; cin >> index >> val;
            data[index-1].push_back(val);
        }
        //(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;
    }
}
0