結果

問題 No.1008 Bench Craftsman
ユーザー ShibuyapShibuyap
提出日時 2020-03-06 22:41:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,836 ms / 2,000 ms
コード長 2,439 bytes
コンパイル時間 1,998 ms
コンパイル使用メモリ 170,184 KB
実行使用メモリ 10,116 KB
最終ジャッジ日時 2024-04-22 09:52:16
合計ジャッジ時間 27,751 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 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 AC 1,750 ms
9,980 KB
testcase_06 AC 153 ms
5,504 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 7 ms
5,376 KB
testcase_09 AC 920 ms
8,428 KB
testcase_10 AC 1,620 ms
9,980 KB
testcase_11 AC 1,836 ms
10,116 KB
testcase_12 AC 1,774 ms
10,116 KB
testcase_13 AC 1,506 ms
10,108 KB
testcase_14 AC 1,481 ms
9,980 KB
testcase_15 AC 93 ms
6,656 KB
testcase_16 AC 1,533 ms
10,108 KB
testcase_17 AC 93 ms
6,784 KB
testcase_18 AC 1,719 ms
9,980 KB
testcase_19 AC 1,500 ms
10,108 KB
testcase_20 AC 376 ms
5,488 KB
testcase_21 AC 499 ms
5,548 KB
testcase_22 AC 1,309 ms
8,228 KB
testcase_23 AC 1,380 ms
8,888 KB
testcase_24 AC 1,433 ms
8,720 KB
testcase_25 AC 213 ms
5,428 KB
testcase_26 AC 181 ms
5,376 KB
testcase_27 AC 1,046 ms
7,652 KB
testcase_28 AC 750 ms
6,916 KB
testcase_29 AC 1,415 ms
9,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("YES");}else{puts("NO");}
#define MAX_N 200005

int main() {
    int n, m;
    cin >> n >> m;
    ll a[n];
    rep(i,n)cin >> a[i];
    ll x[m], w[m];
    rep(i,m){
        cin >> x[i] >> w[i];
        x[i]--;
    }

    ll mi = 1001001001;
    ll ma = 0;

    ll ww[n] = {};
    rep(i,m){
        ww[x[i]] += w[i];
    }
    rep(i,n){
        if(ww[i] >= a[i]){
            cout << "-1" << endl;
            return 0;
        }
    }

    rep(i,m){
        if(w[i] >= a[x[i]]){
            cout << "-1" << endl;
            return 0;
        }
        ma += w[i];
    }
    rep(i,n)mi = min(mi, a[i]);

    if(mi > ma){
        cout << 0 << endl;
        return 0;
    }

    ll l = 0;
    ll r = 1001001001;

    while(l + 1 < r){
        ll c = (l + r) / 2;
        priority_queue<P, vector<P>, greater<P>> que1, que2, que3;
        rep(i,m){
            ll d = w[i] / c;
            P p;
            p.first = max(x[i] - d, (ll)0);
            p.second = i;
            que1.push(p);
            p.first = x[i];
            que2.push(p);
            p.first = x[i] + d;
            que3.push(p);
        }

        int flag = 0;
        ll cnt1 = 0;
        ll cnt2 = 0;

        ll now = 0;
        rep(i,n){
            now += c * cnt1;
            now -= c * cnt2;

            while(que1.size() && que1.top().first <= i){
                P p = que1.top();
                que1.pop();
                now += w[p.second];
                now -= c * (x[p.second] - (ll)i);
                cnt1++;
            }

            while(que2.size() && que2.top().first <= i){
                cnt1--;
                cnt2++;
                que2.pop();
            }

            if(now >= a[i]){
                flag = 1;
                break;
            }

            while(que3.size() && que3.top().first <= i){
                P p = que3.top();
                que3.pop();
                now -= w[p.second];
                now += c * ((ll)i - x[p.second]);
                cnt2--;
            }
            
            

        }


        if(flag == 0){
            r = c;
        }else{
            l = c;
        }
    }
    
    cout << r << endl;
    return 0;
}
 

 
0