結果

問題 No.1008 Bench Craftsman
ユーザー betrue12
提出日時 2020-03-06 22:37:27
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 211 ms / 2,000 ms
コード長 1,233 bytes
コンパイル時間 2,603 ms
コンパイル使用メモリ 196,776 KB
最終ジャッジ日時 2025-01-09 05:24:50
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main(){
    int N, M;
    cin >> N >> M;
    vector<int> A(N);
    for(int i=0; i<N; i++) cin >> A[i];
    vector<int> X(M), W(M);
    for(int i=0; i<M; i++) cin >> X[i] >> W[i], X[i]--;

    int64_t sum = accumulate(W.begin(), W.end(), 0LL);
    if(sum < *min_element(A.begin(), A.end())){
        cout << 0 << endl;
        return 0;
    }

    auto check = [&](int64_t C)->bool{
        vector<int64_t> imos(N+2);
        for(int i=0; i<M; i++){
            int d = W[i]/C;
            int L = max(0, X[i]-d);
            int R = min(N-1, X[i]+d);
            int64_t VL = W[i] - (X[i]-L)*C;
            int64_t VR = W[i] - (R-X[i])*C;
            imos[L] += VL;
            imos[L+1] += C - VL;
            imos[X[i]+1] += -2*C;
            imos[R+1] += C - VR;
            imos[R+2] += VR;
        }
        for(int t=0; t<2; t++) for(int i=1; i<N; i++) imos[i] += imos[i-1];
        for(int i=0; i<N; i++) if(imos[i] >= A[i]) return false;
        return true;
    };

    int ok = 1e9, ng = 0;
    while(ok-ng>1){
        int mid = (ok+ng)/2;
        (check(mid) ? ok : ng) = mid;
    }
    int ans = ok;
    if(ans == 1e9) ans = -1;
    cout << ans << endl;
    return 0;
}
0