Submission #337251

#TimeUsernameProblemLanguageResultExecution timeMemory
337251boykutBigger segments (IZhO19_segments)C++14
100 / 100
102 ms20060 KiB
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define MAXN 500001 #define int long long int a[MAXN], dp[MAXN]; int prefsum[MAXN]; signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; prefsum[i] = prefsum[i - 1] + a[i]; } priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q; q.push({0,0}); int tmp=0; for(int i = 1; i <= n; i++){ while (!q.empty() && q.top().ff <= prefsum[i]) { if (dp[q.top().ss] > dp[tmp]) { tmp = q.top().ss; } else if (dp[q.top().ss] == dp[tmp]) { tmp = max(tmp, q.top().ss); } q.pop(); } q.push({prefsum[i] * 2 - prefsum[tmp], i}); dp[i] = dp[tmp] + 1; } cout << dp[n] << '\n'; 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...