結果

問題 No.1008 Bench Craftsman
ユーザー MisterMister
提出日時 2020-03-06 22:10:52
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 308 ms / 2,000 ms
コード長 1,988 bytes
コンパイル時間 1,085 ms
コンパイル使用メモリ 133,196 KB
実行使用メモリ 6,548 KB
最終ジャッジ日時 2024-05-07 20:39:45
合計ジャッジ時間 6,926 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 308 ms
6,548 KB
testcase_06 AC 49 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 11 ms
5,376 KB
testcase_09 AC 220 ms
5,376 KB
testcase_10 AC 235 ms
6,420 KB
testcase_11 AC 229 ms
6,416 KB
testcase_12 AC 227 ms
6,420 KB
testcase_13 AC 230 ms
6,288 KB
testcase_14 AC 221 ms
6,540 KB
testcase_15 AC 153 ms
6,416 KB
testcase_16 AC 171 ms
6,420 KB
testcase_17 AC 149 ms
6,288 KB
testcase_18 AC 163 ms
6,420 KB
testcase_19 AC 162 ms
6,416 KB
testcase_20 AC 87 ms
5,376 KB
testcase_21 AC 111 ms
5,376 KB
testcase_22 AC 229 ms
5,376 KB
testcase_23 AC 262 ms
5,740 KB
testcase_24 AC 271 ms
5,568 KB
testcase_25 AC 58 ms
5,376 KB
testcase_26 AC 48 ms
5,376 KB
testcase_27 AC 195 ms
5,376 KB
testcase_28 AC 164 ms
5,376 KB
testcase_29 AC 273 ms
5,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

using lint = long long;
constexpr lint INF = 1LL << 30;

void solve() {
    int n, m;
    std::cin >> n >> m;

    std::vector<lint> ots(n);
    for (auto& t : ots) std::cin >> t;

    std::vector<std::pair<int, lint>> ps(m);
    for (auto& p : ps) {
        std::cin >> p.first >> p.second;
        --p.first;
    }
    std::sort(ps.begin(), ps.end());

    lint ok = INF, ng = -1;
    while (ok - ng > 1) {
        lint c = (ok + ng) / 2;
        auto ts = ots;

        MinHeap<lint> que;
        lint sum = 0;
        int j = 0;
        for (int i = 0; i < n; ++i) {
            while (j < m && ps[j].first <= i) {
                lint w = ps[j].second + c * ps[j].first;
                sum += w;
                que.push(w);
                ++j;
            }

            while (!que.empty() && que.top() - c * i <= 0) {
                sum -= que.top();
                que.pop();
            }

            ts[i] -= sum - c * i * que.size();
        }

        while (!que.empty()) que.pop();
        sum = 0;
        j = m - 1;
        for (int i = n - 1; i >= 0; --i) {
            while (j >= 0 && ps[j].first > i) {
                lint w = ps[j].second - c * ps[j].first;
                sum += w;
                que.push(w);
                --j;
            }

            while (!que.empty() && que.top() + c * i < 0) {
                sum -= que.top();
                que.pop();
            }

            ts[i] -= sum + c * i * que.size();
        }

        if (std::any_of(ts.begin(), ts.end(), [](auto x) { return x <= 0; })) {
            ng = c;
        } else {
            ok = c;
        }
    }

    std::cout << (ok == INF ? -1 : ok) << std::endl;
}

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

    solve();

    return 0;
}
0