結果

問題 No.1008 Bench Craftsman
ユーザー mugen_1337mugen_1337
提出日時 2020-03-07 18:13:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,308 ms / 2,000 ms
コード長 2,684 bytes
コンパイル時間 1,801 ms
コンパイル使用メモリ 175,516 KB
実行使用メモリ 17,764 KB
最終ジャッジ日時 2024-04-23 05:07:33
合計ジャッジ時間 20,732 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 1,271 ms
17,268 KB
testcase_06 AC 116 ms
10,880 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 146 ms
6,940 KB
testcase_10 AC 1,289 ms
15,712 KB
testcase_11 AC 1,308 ms
15,872 KB
testcase_12 AC 1,294 ms
15,792 KB
testcase_13 AC 1,285 ms
17,000 KB
testcase_14 AC 1,268 ms
17,264 KB
testcase_15 AC 34 ms
6,944 KB
testcase_16 AC 1,246 ms
15,588 KB
testcase_17 AC 29 ms
6,944 KB
testcase_18 AC 1,276 ms
15,852 KB
testcase_19 AC 1,270 ms
17,764 KB
testcase_20 AC 340 ms
9,872 KB
testcase_21 AC 388 ms
9,580 KB
testcase_22 AC 708 ms
8,924 KB
testcase_23 AC 947 ms
12,056 KB
testcase_24 AC 919 ms
10,776 KB
testcase_25 AC 204 ms
11,972 KB
testcase_26 AC 160 ms
9,332 KB
testcase_27 AC 545 ms
7,344 KB
testcase_28 AC 600 ms
12,196 KB
testcase_29 AC 975 ms
13,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define INF 1000000000
#define mod 1000000007
using ll=long long;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

int n,m;

vector<ll> a,w;
vector<int> x;
vector<ll> costsum;

bool f(ll c){
    vector<ll> cost(n,0);



    // dec
    {
        vector<vector<ll>> in(n);
        vector<vector<ll>> out(n);
        ll sum=0,cnt=0;
        rep(i,m){
            in[x[i]].push_back(w[i]);
            out[min(n-1,x[i]+(int)(w[i]/c))].push_back(w[i]%c);
        }
        rep(i,n){
            for(auto x:in[i]){
                sum+=x;
                cnt++;
            }
            cost[i]+=sum;
            for(auto x:out[i]){
                cnt--;
                sum-=x;
            }
            sum-=c*cnt;
        }
    }
    //inc
    {
        vector<vector<ll>> in(n);
        vector<vector<ll>> out(n);
        ll sum=0,cnt=0;
        rep(i,m){
            in[x[i]].push_back(w[i]);
            out[max(0,x[i]-(int)(w[i]/c))].push_back(w[i]%c);
        }
        for(int i=n-1;i>=0;i--){
            for(auto x:in[i]){
                sum+=x;
                cnt++;
            }
            cost[i]+=sum;
            for(auto x:out[i]){
                cnt--;
                sum-=x;
            }
            sum-=c*cnt;
        }
    }
    // cout<<endl;
    // debug(cost);
    rep(i,n){
        if(cost[i]-costsum[i]>=a[i]){
            // cout<<c<<" false\n";
            return false;
        }
    }
    // cout<<c<<" true\n";
    return true;
}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(0);

    cin>>n>>m;
    a.resize(n);w.resize(m);x.resize(m);costsum.resize(n,0);
    rep(i,n)cin>>a[i];
    rep(i,m){
        cin>>x[i]>>w[i];x[i]--;
        costsum[x[i]]+=w[i];
    }

    //ここやばいて
    rep(i,n){
        ll sum=0;
        if(costsum[i]>=a[i]){
            cout<<-1<<endl;
            return 0;
        }
    }

    {
        ll sum=0;
        rep(i,m) sum+=w[i];
        if(*min_element(ALL(a))>=sum){
            cout<<0<<endl;
            return 0;
        }
    }

    ll lw=1,hi=INF,ans=INF;
    while(lw<=hi){
        ll mid=(lw+hi)/2;
        if(f(mid)){
            chmin(ans,mid);
            hi=mid-1;
        }else{
            lw=mid+1;
        }
    }
    cout<<ans<<endl;
    return 0;
}
0