結果

問題 No.1495 パンの仕入れ
ユーザー Mister
提出日時 2021-05-01 11:53:39
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 1,256 bytes
コンパイル時間 643 ms
コンパイル使用メモリ 72,552 KB
最終ジャッジ日時 2025-01-21 05:04:20
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;
using lint = long long;

constexpr lint INF = 1LL << 60;

void solve() {
    int n, m;
    lint k;
    cin >> n >> m >> k;

    vector<lint> ds(n, 0), first(n, 0);
    lint ans = 0;

    while (m--) {
        int x;
        lint y;
        cin >> x >> y;
        --x;

        ds[x] += 2;
        first[x] += y * 2 - 1;
        ans += y * y;
    }

    lint ok = -INF, ng = INF;
    while (ng - ok > 1) {
        lint mid = (ok + ng) / 2;

        lint cnt = 0;
        for (int i = 0; i < n && cnt <= k; ++i) {
            if (first[i] < mid) continue;

            if (ds[i] == 0) {
                cnt = k + 1;
            } else {
                cnt += (first[i] - mid) / ds[i] + 1;
            }
        }

        if (cnt >= k) {
            ok = mid;
        } else {
            ng = mid;
        }
    }

    for (int i = 0; i < n; ++i) {
        if (first[i] < ng) continue;

        lint c = (first[i] - ng) / ds[i] + 1;
        k -= c;
        ans -= c * (first[i] * 2 - (c - 1) * ds[i]) / 2;
    }
    ans -= k * ok;

    cout << ans << "\n";
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int q;
    cin >> q;
    while (q--) solve();

    return 0;
}
0