Submission #159081

#TimeUsernameProblemLanguageResultExecution timeMemory
159081mrboorgerBigger segments (IZhO19_segments)C++17
100 / 100
529 ms46364 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> //#include "interactive.h" #define pb push_back #define F first #define S second #define ll long long #define ld long double #define sqr(x) (x) * (x) //#define all(a) a.begin(), a.end() using namespace std; const int maxn = 500005; const ll inf = 1e18; vector <pair <int, ll>> dp[maxn]; int a[maxn]; ll sum[maxn]; ll f(int l, int r) { return sum[r] - (l > 0 ? sum[l - 1] : 0); } int main() { #ifdef LOCAL // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif // LOCAL // ios_base::sync_with_stdio(0); // cin.tie(0); // cout.tie(0); int n; cin >> n; for(int i = 0; i < n; ++i) { cin >> a[i]; sum[i] = a[i] + (i > 0 ? sum[i - 1] : 0); } for(int i = 0; i < n; ++i) { dp[i].pb({0, inf}); dp[i].pb({0, inf}); } dp[0][0] = {1, a[0]}; for(int i = 0; i < n - 1; ++i) for(int j = 0; j < 2; ++j) { if (dp[i][j].S == inf) continue; if (dp[i][j].F > dp[i + 1][0].F) { swap(dp[i + 1][0], dp[i + 1][1]); dp[i + 1][0] = {dp[i][j].F, dp[i][j].S + a[i + 1]}; } else if (dp[i][j].F == dp[i + 1][0].F && dp[i][j].S + a[i + 1] < dp[i + 1][0].S) { dp[i + 1][0].S = dp[i][j].S + a[i + 1]; } else if (dp[i][j].F > dp[i + 1][1].F) { dp[i + 1][1] = {dp[i][j].F, dp[i][j].S + a[i + 1]}; } else if (dp[i][j].F == dp[i + 1][1].F && dp[i][j].S + a[i + 1] < dp[i + 1][1].S) { dp[i + 1][1].S = dp[i][j].S + a[i + 1]; } { int l = i + 1, r = n - 1; int b = n + 1; while(l <= r) { int m = (l + r) / 2; if (f(i + 1, m) >= dp[i][j].S) { b = min(b, m); r = m - 1; } else { l = m + 1; } } if (b < n + 1) { if (dp[i][j].F + 1 > dp[b][0].F) { swap(dp[b][0], dp[b][1]); dp[b][0] = {dp[i][j].F + 1, f(i + 1, b)}; } else if (dp[i][j].F + 1 == dp[b][0].F && f(i + 1, b) < dp[b][0].S) { dp[b][0].S = f(i + 1, b); } else if (dp[i][j].F + 1 > dp[b][1].F) { dp[b][1] = {dp[i][j].F + 1, f(i + 1, b)}; } else if (dp[i][j].F + 1 == dp[b][1].F && f(i + 1, b) < dp[b][1].S) { dp[b][1].S = f(i + 1, b); } } } } cout << max(dp[n - 1][0].F, dp[n - 1][1].F) << endl; 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...