# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
384330 | vanic | Bigger segments (IZhO19_segments) | C++14 | 2 ms | 2412 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn=3005;
ll a[maxn];
ll pref[maxn];
ll dp[maxn][maxn];
int gran[maxn];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i=0; i<n; i++){
cin >> a[i];
pref[i+1]=pref[i]+a[i];
}
dp[0][1]=a[0];
for(int i=1; i<n; i++){
for(int j=1; j<=n; j++){
if(!dp[i-1][j]){
while(pref[i+1]-pref[gran[j-1]+2]>=dp[gran[j-1]+1][j-1]){
gran[j-1]++;
}
if(dp[gran[j-1]][j-1]<=pref[i+1]-pref[gran[j-1]+1]){
dp[i][j]=pref[i+1]-pref[gran[j-1]+1];
// cout << "gradim " << j << ' ' << i << endl;
gran[j]=i;
}
break;
}
else if(j>1){
while(pref[i+1]-pref[gran[j-1]+2]>=dp[gran[j-1]+1][j-1]){
gran[j-1]++;
}
dp[i][j]=pref[i+1]-pref[gran[j-1]+1];
if(dp[gran[j]][j]-pref[i+1]+pref[gran[j]+1]>dp[i][j]){
gran[j]=i;
}
}
else{
dp[i][j]=pref[i+1];
}
}
}
int br;
for(int i=1; i<=n; i++){
if(dp[n-1][i]){
br=i;
}
}
cout << br << '\n';
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |