結果

問題 No.1008 Bench Craftsman
ユーザー nrvftnrvft
提出日時 2020-03-07 00:00:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 638 ms / 2,000 ms
コード長 2,951 bytes
コンパイル時間 2,587 ms
コンパイル使用メモリ 209,512 KB
実行使用メモリ 16,484 KB
最終ジャッジ日時 2024-04-22 11:00:04
合計ジャッジ時間 13,097 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 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 536 ms
16,364 KB
testcase_06 AC 100 ms
10,232 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 131 ms
8,828 KB
testcase_10 AC 560 ms
16,256 KB
testcase_11 AC 614 ms
16,356 KB
testcase_12 AC 605 ms
16,256 KB
testcase_13 AC 554 ms
16,484 KB
testcase_14 AC 602 ms
16,256 KB
testcase_15 AC 501 ms
16,256 KB
testcase_16 AC 539 ms
16,356 KB
testcase_17 AC 593 ms
16,424 KB
testcase_18 AC 638 ms
16,440 KB
testcase_19 AC 622 ms
16,256 KB
testcase_20 AC 180 ms
9,528 KB
testcase_21 AC 195 ms
8,652 KB
testcase_22 AC 331 ms
10,064 KB
testcase_23 AC 458 ms
12,952 KB
testcase_24 AC 445 ms
12,152 KB
testcase_25 AC 107 ms
9,392 KB
testcase_26 AC 94 ms
7,936 KB
testcase_27 AC 253 ms
8,636 KB
testcase_28 AC 295 ms
11,196 KB
testcase_29 AC 467 ms
14,088 KB
権限があれば一括ダウンロードができます

ソースコード

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};
    }
    vl Xcnt(N);
    sort(all(pp));
    rep(i, M) 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