結果

問題 No.1008 Bench Craftsman
ユーザー ShibuyapShibuyap
提出日時 2020-03-06 22:44:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,550 ms / 2,000 ms
コード長 2,460 bytes
コンパイル時間 1,603 ms
コンパイル使用メモリ 172,912 KB
実行使用メモリ 10,044 KB
最終ジャッジ日時 2023-08-04 12:21:49
合計ジャッジ時間 24,543 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1,503 ms
10,044 KB
testcase_06 AC 121 ms
5,288 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 905 ms
8,408 KB
testcase_10 AC 1,394 ms
9,968 KB
testcase_11 AC 1,550 ms
10,008 KB
testcase_12 AC 1,493 ms
9,892 KB
testcase_13 AC 1,257 ms
9,896 KB
testcase_14 AC 1,258 ms
9,992 KB
testcase_15 AC 33 ms
6,736 KB
testcase_16 AC 1,309 ms
9,996 KB
testcase_17 AC 33 ms
6,648 KB
testcase_18 AC 1,475 ms
10,004 KB
testcase_19 AC 1,284 ms
9,948 KB
testcase_20 AC 313 ms
5,288 KB
testcase_21 AC 428 ms
5,388 KB
testcase_22 AC 1,141 ms
7,888 KB
testcase_23 AC 1,236 ms
8,816 KB
testcase_24 AC 1,265 ms
8,684 KB
testcase_25 AC 167 ms
5,096 KB
testcase_26 AC 143 ms
4,752 KB
testcase_27 AC 935 ms
7,536 KB
testcase_28 AC 650 ms
6,772 KB
testcase_29 AC 1,236 ms
9,196 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)scanf("%lld", &a[i]);
    ll x[m], w[m];
    rep(i,m){
        scanf("%lld%lld", &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