# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
538674 | nicholask | Split the sequence (APIO14_sequence) | C++14 | 71 ms | 131072 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 <bits/stdc++.h>
#define int long long
using namespace std;
int dp[100001][201];
int bac[100001][201];
signed main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n,k;
cin>>n>>k;
k++;
int a[n+1];
for (int i=1; i<=n; i++) cin>>a[i];
int ps[n+1]={};
for (int i=1; i<=n; i++) ps[i]=ps[i-1]+a[i];
for (int j=1; j<=k; j++){
deque <int> q;
if (j==1) q.push_back(0);
for (int i=1; i<=n; i++){
while (q.size()>=2&&dp[q[1]][j-1]-dp[q[0]][j-1]>(ps[n]-ps[i])*(ps[q[1]]-ps[q[0]])) q.pop_front();
if (q.size()) dp[i][j]=dp[q[0]][j-1]+(ps[i]-ps[q[0]])*(ps[n]-ps[i]);
if (q.size()) bac[i][j]=q[0];
while (q.size()>=2&&(dp[q.back()][j-1]-dp[q[q.size()-2]][j-1])*(ps[i]-ps[q.back()])<(dp[i][j-1]-dp[q.back()][j-1])*(ps[q.back()]-ps[q[q.size()-2]])) q.pop_back();
q.push_back(i);
}
}
cout<<dp[n][k]<<'\n';
vector <int> v;
v.push_back(n);
for (int i=k; i>1; i--) v.push_back(bac[v.back()][i]);
reverse(v.begin(),v.end());
for (int i=0; i+1<v.size(); i++) cout<<v[i]<<" \n"[i+2==v.size()];
}
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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |