Submission #345627

#TimeUsernameProblemLanguageResultExecution timeMemory
345627vonatlusBigger segments (IZhO19_segments)C++14
0 / 100
1 ms364 KiB
#include <bits/stdc++.h> #define all(x) x.begin(), x.end() #define y second #define x first using namespace std; using ll = long long; using pii = pair<ll, ll>; const int N = 5e5+10; void fin() { #ifdef AM freopen(".in", "r", stdin); #endif } ll p[N], a[N]; pii dp[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr), fin(); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) p[i] += p[i - 1] + a[i]; stack<pair<ll, ll>> s; s.push({0, 0}); for (int i = 1; i <= n; i++) { while (!s.empty() && dp[s.top().x].y + s.top().y <= p[i]) { if (dp[s.top().x].x+1 == dp[i].x) { dp[i].y = min(dp[i].y, s.top().y); } if (dp[s.top().x].x+1 > dp[i].x) { dp[i] = pii{dp[s.top().x].x+1, p[i]-s.top().y}; } s.pop(); } s.push({i, p[i]}); } cout << dp[n].x; 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...