# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
374122 | mariowong | Bigger segments (IZhO19_segments) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
#define push_back emplace_back
#define push_front emplace_front
#define pii pair<int,int>
#define ff first
#define ss second
using namespace std;
#define debug(args...) kout("[ " + string(#args) + " ]", args)
void kout() { cerr << endl; }
template <class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ',kout(b...); }
template <class T> void pary(T L, T R) { while (L != R) cerr << *L << " \n"[++L==R]; }
const int MAXN = 500000;
int n;
int a[MAXN+1];
int pre[MAXN+1];
int dp[MAXN+1];
int len[MAXN+1];
deque <pii> deq;
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i], pre[i] = pre[i-1] + a[i];
deq.push_back(0, 0);
pii tmp;
for (int i = 1; i <= n; i++) {
while (deq.size() && pre[i]-pre[def.front().ss] >= deq.front().ff) {
dp[i] = dp[deq.front().ss] + 1;
tmp = deq.front();
deq.pop_front();
}
deq.push_front(tmp);
while (deq.size() && deq.back().ff >= pre[i]-pre[def.front().ss])
deq.pop_back();
deq.push_back(pre[i]-pre[def.front().ss], i);
}
cout << dp[n] << '\n';
}