Submission #1351327

#TimeUsernameProblemLanguageResultExecution timeMemory
1351327chithanhnguyenBigger segments (IZhO19_segments)C++20
100 / 100
56 ms14108 KiB
/*
Author: Nguyen Chi Thanh - High School for the Gifted - VNU.HCM (i2528)
*/
#include <bits/stdc++.h>
using namespace std;

/* START OF TEMPALTE */

// #define int long long
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define popcount __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(x) (1ll << (x))
#define SZ(a) ((int32_t)a.size())

#define debug(a, l, r) {for (int _i = (l); _i <= (r); ++_i) cout << (a)[_i] << ' '; cout << '\n';}

template<class X, class Y>
bool minimize(X &x, const Y &y) {
    if (x > y) {
        x = y;
        return true;
    } else return false;
}

template<class X, class Y>
bool maximize(X &x, const Y &y) {
    if (x < y) {
        x = y;
        return true;
    } else return false;
}

/* END OF TEMPALTE */

const int MAXN = 5e5 + 5;
int n, a[MAXN];
ll pref[MAXN];
pll dp[MAXN];

void init() {
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        pref[i] = pref[i - 1] + a[i];
    }
}

void solve() {
    dp[1] = {1, 0};

    for (int i = 1; i <= n; ++i) {
        maximize(dp[i], dp[i - 1]);
        // pref[j] - pref[i] >= pref[i] - dp[i].se
        // pref[j] >= 2 * pref[i] - dp[i].se
        int j = lower_bound(pref + 1, pref + n + 1, 2 * pref[i] - dp[i].se) - pref;
        maximize(dp[j], pll{dp[i].fi + 1, pref[i]});
    }

    cout << dp[n].fi;
}

signed main() {
    #ifdef NCTHANH
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);

    init();
    solve();

    return 0;
}
#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...