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 f first
#define s second
#define fastio ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define ll long long
using namespace std;
const int mxN = 1e6 + 45, mod = 1e9 + 7;
const ll inf = 2e18 + 43;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, a[mxN], dp[mxN];
ll p[mxN];
set <pair <ll, int> > st;
void add(int idx, ll sum) {
if (st.empty()) {
st.insert({sum, idx});
return;
}
auto it = st.lower_bound({sum + 1, 0});
vector <pair <ll, int> > del;
while (it != st.end()) del.push_back(*(it++));
for (auto x : del) st.erase(x);
st.insert({sum, idx});
}
int get(int idx, ll sum) {
auto it = st.lower_bound({sum + 1, 0});
it--;
return it -> second;
}
int main() {
fastio;
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i], p[i] = p[i - 1] + a[i];
add(0, 0);
for (int i = 1; i <= n; ++i) {
int L = get(i, p[i]);
dp[i] = dp[L] + 1;
add(i, p[i] * 2 - p[L]);
}
cout << dp[n];
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |