結果

問題 No.1008 Bench Craftsman
ユーザー nrvftnrvft
提出日時 2020-03-06 23:40:35
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
RE  
実行時間 -
コード長 2,948 bytes
コンパイル時間 2,423 ms
コンパイル使用メモリ 207,216 KB
最終ジャッジ日時 2025-01-09 05:38:55
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 15 WA * 5 RE * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

const ll MOD = 1e9 + 7;
const ll INF = 1e16;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)

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

void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}
//--------------------------------------------------------------------------------//

int main() {
    init();
    ll N,M;cin>>N>>M;
    vl A(N);
    rep(i, N) cin >> A[i];
    vc<pair<ll,ll>> pp(M);
    rep(i,M){
        ll x, w;
        cin >> x >> w;
        x--;
        pp[i] = {x, w};
    }
    sort(all(pp));
    vl Xcnt(N);
    rep(i, N) Xcnt[pp[i].first]++;

    //c=0でokなら返す
    ll sum = 0;
    rep(i,M){
        sum += pp[i].second;
    }
    bool ok0 = true;
    rep(i,N){
        if(sum>=A[i]){
            ok0 = false;
            break;
        }
    }
    if(ok0){
        cout << 0 << endl;
        return 0;
    }

    //二分探索
    ll ng = 0, ok = 1e9;
    while(ok-ng>1){
        bool isok = true;
        ll mid = (ok + ng) / 2;

        vvc<pair<ll,ll>> add(N), del(N);
        for(auto p:pp){
            ll x, w;
            tie(x, w) = p;
            ll r = w / mid;
            add[max(0ll, x - r)].eb(x,w);
            del[min(N - 1, x + r)].eb(x,w);
        }
        // cout << "add" << endl;
        // rep(i,N){
        //     cout << i << endl;
        //     for (auto p : add[i]) cout << p.first << " " << p.second << " ";
        //     cout << endl;
        // }
        // cout << "del" << endl;
        // rep(i,N){
        //     cout << i << endl;
        //     for (auto p : del[i]) cout << p.first << " " << p.second << " ";
        //     cout << endl<<endl;
        // }

        ll cnt = 0, val = 0;
        rep(i,N){
            val += cnt * mid;
            for(auto p:add[i]){
                ll x, w;
                tie(x, w) = p;
                cnt++;
                val += w - (x - i) * mid;
            }
            if(val>=A[i]){
                isok = false;
                break;
            }
            for(auto p:del[i]){
                ll x, w;
                tie(x, w) = p;
                cnt++;
                val -= w - (i - x) * mid;
            }
            cnt -= Xcnt[i] * 2;
        }
        (isok ? ok : ng) = mid;
    }
    if(ok==1e9){
        cout << -1 << endl;
        return 0;
    }
    cout << ok << endl;
}
0