Submission #1082247

#TimeUsernameProblemLanguageResultExecution timeMemory
1082247ByeWorldBigger segments (IZhO19_segments)C++14
0 / 100
1 ms448 KiB
#include <bits/stdc++.h> #pragma GCC optimize("O3") #define int long long #define ll long long #define pb push_back #define fi first #define se second #define lf (id<<1) #define rg ((id<<1)|1) #define md ((l+r)>>1) using namespace std; typedef pair<int,int> pii; typedef pair<int,pii> ipii; const int MAXN = 5e5+15; const int INF = 1e9+10; const int MOD = 1e9+7; /* dp[i] : max segment [1..i] pref[i] : prefix sum [1..i] opt[i] : min last segment untuk mendapatkan dp[i] for (int i=1;i<=N;i++) { for (int j=i-1;j>=0;j--) { if (opt[j] <= pref[i]-pref[j]) { dp[i] = dp[j]+1; opt[i] = pref[i]-pref[j]; break; } } } */ int n; int a[MAXN], dp[MAXN], pr[MAXN], opt[MAXN]; signed main(){ cin >> n; for(int i=1; i<=n; i++){ cin >> a[i]; pr[i] = pr[i-1]+a[i]; } int las = 0; for(int i=1; i<=n; i++){ while(las+1<=i-1 && opt[las+1] <= pr[i]-pr[las+1]){ las++; } dp[i] = dp[las]+1; opt[i] = pr[i]-pr[las]; // cout << i << ' ' << dp[i] << ' ' << opt[i] << " pp\n"; // for(int j=i-1; j>=0; j--){ // if(opt[j] <= pr[i]-pr[j]){ // dp[i] = dp[j]+1; // opt[i] = pr[i]-pr[j]; // break; // } // } } cout << dp[n] << '\n'; }
#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...