結果

問題 No.1008 Bench Craftsman
ユーザー mugen_1337mugen_1337
提出日時 2020-10-24 20:51:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 355 ms / 2,000 ms
コード長 1,284 bytes
コンパイル時間 2,255 ms
コンパイル使用メモリ 204,164 KB
実行使用メモリ 9,824 KB
最終ジャッジ日時 2023-09-28 20:55:42
合計ジャッジ時間 9,168 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 353 ms
9,820 KB
testcase_06 AC 128 ms
8,112 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 6 ms
4,376 KB
testcase_09 AC 147 ms
4,512 KB
testcase_10 AC 355 ms
9,696 KB
testcase_11 AC 349 ms
9,644 KB
testcase_12 AC 348 ms
9,652 KB
testcase_13 AC 344 ms
9,696 KB
testcase_14 AC 348 ms
9,696 KB
testcase_15 AC 87 ms
6,260 KB
testcase_16 AC 345 ms
9,644 KB
testcase_17 AC 88 ms
6,068 KB
testcase_18 AC 342 ms
9,824 KB
testcase_19 AC 342 ms
9,692 KB
testcase_20 AC 120 ms
6,964 KB
testcase_21 AC 119 ms
6,128 KB
testcase_22 AC 166 ms
5,540 KB
testcase_23 AC 254 ms
7,592 KB
testcase_24 AC 236 ms
6,740 KB
testcase_25 AC 127 ms
7,752 KB
testcase_26 AC 98 ms
6,764 KB
testcase_27 AC 128 ms
4,940 KB
testcase_28 AC 202 ms
7,576 KB
testcase_29 AC 274 ms
8,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define INF 1000000000
using ll=long long;
int n,m,x[100010];
ll wsum[100010],a[100010],w[100010];
bool f(ll c){
    vector<ll> in(n,0),ic(n,0),out(n,0),oc(n,0),cost(n,0);
    ll sum=0,cnt=0;
    rep(i,m){in[x[i]]+=w[i];ic[x[i]]++;out[min(n-1,x[i]+(int)(w[i]/c))]+=w[i]%c;oc[min(n-1,x[i]+(int)(w[i]/c))]++;}
    rep(i,n){sum+=in[i];cnt+=ic[i]-oc[i];cost[i]+=sum;sum-=out[i]+c*cnt;}
    in.assign(n,0);ic.assign(n,0);out.assign(n,0);oc.assign(n,0);
    sum=0,cnt=0;
    rep(i,m){
        in[x[i]]+=w[i];ic[x[i]]++;
        out[max(0,x[i]-(int)(w[i]/c))]+=w[i]%c;oc[max(0,x[i]-(int)(w[i]/c))]++;
    }
    for(int i=n-1;i>=0;i--){sum+=in[i];cnt+=ic[i]-oc[i];cost[i]+=sum;sum-=out[i]+c*cnt;}
    rep(i,n)if(cost[i]-wsum[i]>=a[i])return false;
    return true;
}
signed main(){
    cin>>n>>m;
    rep(i,n)cin>>a[i];
    rep(i,m){cin>>x[i]>>w[i];x[i]--;wsum[x[i]]+=w[i];}
    rep(i,n)if(wsum[i]>=a[i]){cout<<-1<<endl;return 0;}
    ll sum=0,lw=1,hi=INF,ans=INF;
    rep(i,m) sum+=w[i];
    if(*min_element(a,a+n)>=sum){
        cout<<0<<endl;
        return 0;
    }
    while(lw<=hi){
        ll M=(lw+hi)/2;
        if(f(M)){
            ans=M;hi=M-1;
        }else lw=M+1;
    }
    cout<<ans<<endl;
}
0