제출 #1008499

#제출 시각아이디문제언어결과실행 시간메모리
1008499Alihan_8Bigger segments (IZhO19_segments)C++17
100 / 100
52 ms16980 KiB
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long

using i64 = long long;

template <class F, class _S>
bool chmin(F &u, const _S &v){
    bool flag = false;
    if ( u > v ){
        u = v; flag |= true;
    }
    return flag;
}

template <class F, class _S>
bool chmax(F &u, const _S &v){
    bool flag = false;
    if ( u < v ){
        u = v; flag |= true;
    }
    return flag;
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n; cin >> n;

    vector <int> pf(n + 2);

    for ( int i = 0; i < n; i++ ){
        int x; cin >> x;

        pf[i + 1] = pf[i] + x;
    }

    vector <int> dp(n + 1), opt(n + 2);

    for ( int i = 1; i <= n; i++ ){
        if ( chmax(dp[i], dp[i - 1]) ){
            opt[i] = opt[i - 1];
        } else if ( dp[i] == dp[i - 1] ){
            chmax(opt[i], opt[i - 1]);
        }

        dp[i] = dp[opt[i]] + 1;

        int df = pf[i] * 2 - pf[opt[i]];

        int l = i + 1, r = n + 1;

        while ( l < r ){
            int md = (l + r) >> 1;

            if ( pf[md] < df ){
                l = md + 1;
            } else r = md;
        }

        if ( chmax(dp[l], dp[i] + 1) ){
            opt[l] = i;
        } else if ( dp[l] == dp[i] + 1 ){
            chmax(opt[l], i);
        }
    }

    cout << dp.back();

    cout << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...