Submission #337250

#TimeUsernameProblemLanguageResultExecution timeMemory
337250boykutBigger segments (IZhO19_segments)C++14
13 / 100
1 ms364 KiB
#include <bits/stdc++.h>

using namespace std;

#define ff first
#define ss second
#define MAXN 500001

int a[MAXN], dp[MAXN];
int prefsum[MAXN];

int 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...