結果

問題 No.1495 パンの仕入れ
ユーザー parukiparuki
提出日時 2021-05-02 21:31:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 185 ms / 2,000 ms
コード長 2,332 bytes
コンパイル時間 2,636 ms
コンパイル使用メモリ 204,308 KB
実行使用メモリ 7,856 KB
最終ジャッジ日時 2023-09-28 12:13:35
合計ジャッジ時間 8,511 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 148 ms
4,376 KB
testcase_03 AC 149 ms
4,380 KB
testcase_04 AC 149 ms
4,376 KB
testcase_05 AC 149 ms
4,376 KB
testcase_06 AC 147 ms
4,376 KB
testcase_07 AC 150 ms
4,376 KB
testcase_08 AC 150 ms
4,380 KB
testcase_09 AC 149 ms
4,376 KB
testcase_10 AC 150 ms
4,380 KB
testcase_11 AC 149 ms
4,380 KB
testcase_12 AC 40 ms
4,380 KB
testcase_13 AC 40 ms
4,380 KB
testcase_14 AC 40 ms
4,380 KB
testcase_15 AC 40 ms
4,380 KB
testcase_16 AC 40 ms
4,376 KB
testcase_17 AC 40 ms
4,376 KB
testcase_18 AC 40 ms
4,380 KB
testcase_19 AC 39 ms
4,376 KB
testcase_20 AC 40 ms
4,376 KB
testcase_21 AC 40 ms
4,380 KB
testcase_22 AC 185 ms
7,776 KB
testcase_23 AC 185 ms
7,856 KB
testcase_24 AC 32 ms
4,380 KB
testcase_25 AC 32 ms
4,376 KB
testcase_26 AC 31 ms
4,376 KB
testcase_27 AC 31 ms
4,380 KB
testcase_28 AC 174 ms
7,856 KB
testcase_29 AC 173 ms
7,736 KB
testcase_30 AC 31 ms
4,376 KB
testcase_31 AC 31 ms
4,380 KB
testcase_32 AC 31 ms
4,376 KB
testcase_33 AC 30 ms
4,376 KB
testcase_34 AC 31 ms
4,380 KB
testcase_35 AC 40 ms
4,376 KB
testcase_36 AC 39 ms
4,380 KB
testcase_37 AC 39 ms
4,376 KB
testcase_38 AC 40 ms
4,376 KB
testcase_39 AC 39 ms
4,380 KB
testcase_40 AC 32 ms
4,376 KB
testcase_41 AC 32 ms
4,376 KB
testcase_42 AC 32 ms
4,380 KB
testcase_43 AC 32 ms
4,376 KB
testcase_44 AC 32 ms
4,380 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

void solve() {
    int T;
    cin >> T;
    while (T--) {
        int N, M, K;
        cin >> N >> M >> K;
        vll S(N), A(N), B(N);
        rep(i, M) {
            int x, y;
            cin >> x >> y;
            --x;
            S[x]++;
            A[x] += y;
            B[x] += (ll)y * y;
        }

        ll LIM = (ll)1e18;
        ll ng = LIM + 10, ok = -(ll)1e15;
        bool zero = find(all(S), 0ll) != S.end() ? true : false;
        if (zero)ng = 1;

        auto f = [&](ll z) {
            ll sm = 0, k = 0, just = 0;
            rep(i, N) {
                if (!S[i])continue;
                ll x = max((z + S[i] + 2 * A[i]) / (2 * S[i]), 0ll);
                k += x;
                sm += S[i] * x * x - 2 * A[i] * x + B[i];

                ll w = 2 * S[i] * x - S[i] - 2 * A[i];
                if (w == z && x > 0) {
                    just++;
                    k--;
                    sm -= w;
                }
                
                if (sm > LIM) {
                    k = K + 1;
                    break;
                }
                if (k > K)break;
            }
            if (k < K && just) {
                ll a = min(K - k, just);
                k += a;
                sm += a * z;
            } else if (just && K == k) {
                k++;
            }
            if (z == 0 && zero && k < K)k = K;
            return pair<ll, ll>(sm, k);
        };

        while (ng - ok > 1) {
            ll z = (ok + ng) / 2;
            ll sm, k;
            tie(sm, k) = f(z);
            (k <= K ? ok : ng) = z;
        }
        cout << f(ok).first << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);
    solve();
    return 0;
}
0