Submission #332446

#TimeUsernameProblemLanguageResultExecution timeMemory
332446dolphingarlicBigger segments (IZhO19_segments)C++14
100 / 100
76 ms18028 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4,avx2,fma,avx")
typedef long long ll;
using namespace std;

ll a[500001], pref[500001];
pair<int, ll> dp[500001];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    deque<int> dq = {0};
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        pref[i] = pref[i - 1] + a[i];
        while (dq.size() != 1 && pref[i] - pref[dq[1]] >= dp[dq[1]].second)
            dq.pop_front();
        dp[i] = {dp[dq[0]].first + 1, pref[i] - pref[dq[0]]};
        while (dp[dq.back()].second - dp[i].second >= pref[i] - pref[dq.back()])
            dq.pop_back();
        dq.push_back(i);
    }
    cout << dp[n].first;
    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...