結果

問題 No.1008 Bench Craftsman
ユーザー milanis48663220milanis48663220
提出日時 2021-05-26 22:21:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,993 bytes
コンパイル時間 1,170 ms
コンパイル使用メモリ 120,172 KB
実行使用メモリ 7,036 KB
最終ジャッジ日時 2024-04-23 15:45:58
合計ジャッジ時間 5,716 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 RE -
testcase_06 AC 75 ms
5,760 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 47 ms
5,376 KB
testcase_10 WA -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 AC 134 ms
6,656 KB
testcase_15 AC 31 ms
6,656 KB
testcase_16 AC 131 ms
6,788 KB
testcase_17 AC 30 ms
6,656 KB
testcase_18 AC 129 ms
6,784 KB
testcase_19 AC 134 ms
6,784 KB
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 AC 69 ms
5,516 KB
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

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

using namespace std;
typedef long long ll;

int n, m;
ll a[100005];
int x[100005];
ll w[100005];

void input(){
    cin >> n >> m;
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    for(int i = 0; i < m; i++){
        cin >> x[i] >> w[i];
        x[i]--;
    }
}

const ll INF = 1e18;

bool judge(ll c){
    if(c == 0){
        ll w_sum = 0;
        for(int i = 0; i < m; i++) w_sum += w[i];
        for(int i = 0; i < n; i++) {
            if(a[i] <= w_sum) return false;
        }
        return true;
    }
    vector<ll> d_load(n+5), load(n+5);
    auto add_d_load = [&](int l, int r, ll d){
        assert(l >= 0);
        // cout << l << ' ' << r << ' ' << d << endl;
        chmin(r, n);
        d_load[l] += d;
        d_load[r] -= d;
    };
    auto add_val = [&](int i, ll x){
        assert(i >= 0);
        assert(i < n);
        if(i == 0){
            load[0] += x;
            d_load[0] -= x;
            d_load[1] += x;
        }else{
            d_load[i-1] += x;
            d_load[i] -= 2*x;
            d_load[i+1] += x;
        }
    };
    for(int j = 0; j < m; j++){
        if(w[j] < c){
            // cout << 'H' << ' ' << x[j] << ' ' << w[j] << endl;
            add_val(x[j], w[j]);
            continue;
        }
        int l = x[j]-w[j]/c;
        if(l <= 0){
            // add_val(0, w[j]%c-c*l);
            load[0] += w[j]%c-c*l;
            l = 0;
        }else{
            add_d_load(l-1, l, w[j]%c);
        }
        add_d_load(x[j]+w[j]/c, x[j]+w[j]/c+1, -(w[j]%c));
        // cout << l << ' ' << w[j] << endl;
        add_d_load(l, x[j], c);
        add_d_load(x[j], x[j]+w[j]/c, -c);
    }
    for(int i = 0; i < n; i++) d_load[i+1] += d_load[i];
    for(int i = 0; i < n; i++) load[i+1] += load[i]+d_load[i];
    for(int i = 0; i < n; i++){
        // cout << load[i] << ' ' << a[i] << endl;
        if(load[i] >= a[i]) return false;
    }
    // debug_value(c);
    return true;
}

void solve(){
    if(judge(0)){
        cout << 0 << endl;
        return;
    }else if(!judge(INF)){
        cout << -1 << endl;
        return;
    }
    ll l = 0, r = INF;
    while(r-l > 1){
        ll c = (l+r)/2;
        // debug_value(c);
        if(judge(c)) r = c;
        else l = c;
    }
    cout << r << endl;
}

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