結果

問題 No.1008 Bench Craftsman
ユーザー kibunakibuna
提出日時 2020-03-06 22:54:38
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 161 ms / 2,000 ms
コード長 2,487 bytes
コンパイル時間 1,487 ms
コンパイル使用メモリ 167,780 KB
実行使用メモリ 7,100 KB
最終ジャッジ日時 2023-08-04 12:29:31
合計ジャッジ時間 5,101 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 161 ms
6,948 KB
testcase_06 AC 56 ms
5,620 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 30 ms
4,624 KB
testcase_10 AC 148 ms
7,080 KB
testcase_11 AC 147 ms
6,952 KB
testcase_12 AC 148 ms
7,068 KB
testcase_13 AC 149 ms
6,964 KB
testcase_14 AC 145 ms
7,100 KB
testcase_15 AC 32 ms
7,096 KB
testcase_16 AC 92 ms
7,004 KB
testcase_17 AC 33 ms
7,004 KB
testcase_18 AC 85 ms
7,004 KB
testcase_19 AC 89 ms
6,924 KB
testcase_20 AC 53 ms
4,856 KB
testcase_21 AC 55 ms
4,720 KB
testcase_22 AC 89 ms
4,916 KB
testcase_23 AC 113 ms
6,008 KB
testcase_24 AC 111 ms
5,712 KB
testcase_25 AC 51 ms
5,320 KB
testcase_26 AC 40 ms
4,780 KB
testcase_27 AC 75 ms
4,544 KB
testcase_28 AC 83 ms
5,528 KB
testcase_29 AC 118 ms
6,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint     = long long;
const lint inf = 1e9;
const lint mod = 1000000007;

template <class T>
bool chmax(T &a, const T &b) {
    return (a < b) ? (a = b, 1) : 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    return (b < a) ? (a = b, 1) : 0;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    lint n, m;
    cin >> n >> m;
    vector<lint> a(n);
    lint amin = inf;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        chmin(amin, a[i]);
    }
    lint wtot = 0;
    vector<lint> x(m), w(m);
    for (int i = 0; i < m; ++i) {
        cin >> x[i] >> w[i];
        x[i]--;
        wtot += w[i];
    }
    if (amin > wtot) {
        cout << 0 << "\n";
        return 0;
    }
    lint ok = inf;
    lint ng = 0;

    auto check = [&](lint mid) {
        vector<lint> imos(n, 0), imos1(n, 0);
        for (int i = 0; i < m; ++i) {
            if (mid * 2 >= w[i]) {
                imos1[x[i]] += w[i];
                if (x[i] - 1 >= 0)
                    imos1[x[i] - 1] += max(0LL, w[i] - mid);
                if (x[i] + 1 <= n - 1)
                    imos1[x[i] + 1] += max(0LL, w[i] - mid);
                continue;
            }
            imos[max(0LL, (x[i] - w[i] / mid + 1))] += mid;
            if (x[i] + 1 <= n - 1)
                imos[x[i] + 1] -= 2 * mid;
            if (x[i] + w[i] / mid + 1 <= n - 1)
                imos[x[i] + w[i] / mid + 1] += mid;
        }
        for (int i = 1; i < n; ++i) {
            imos[i] += imos[i - 1];
        }
        for (int i = 0; i < m; ++i) {
            if (mid * 2 >= w[i])
                continue;
            if (x[i] - w[i] / mid >= 0)
                imos[x[i] - w[i] / mid] += w[i] % mid;
            else if (x[i] - w[i] / mid < 0) {
                imos[0] += w[i] % mid + max(abs(x[i] - w[i] / mid) - 1, 0LL) * mid;
            }
            if (x[i] + w[i] / mid + 1 <= n - 1)
                imos[x[i] + w[i] / mid + 1] -= w[i] % mid;
        }
        for (int i = 1; i < n; ++i) {
            imos[i] += imos[i - 1];
        }
        for (int i = 0; i < n; ++i) {
            if (a[i] <= imos[i] + imos1[i])
                return false;
        }
        return true;
    };
    if (!check(inf)) {
        cout << -1 << "\n";
        return 0;
    }
    while (abs(ok - ng) != 1) {
        lint mid               = (ok + ng) / 2;
        (check(mid) ? ok : ng) = mid;
    }
    cout << ok << "\n";
    return 0;
}
0