# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
235905 | dlwocks31 | Split the sequence (APIO14_sequence) | C++17 | 394 ms | 84216 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 MAXN 100005
using namespace std;
typedef long long ll;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef struct CHT{
ll la[MAXN], lb[MAXN]; int lz[MAXN];
int stkn=0,pt=1;
void insert(ll a, ll b, int z){
while(stkn>pt){
if(a==la[stkn] && b>=lb[stkn]) stkn--;
else if((lb[stkn]-b)*(la[stkn]-la[stkn-1])<(a-la[stkn])*(lb[stkn-1]-lb[stkn])) stkn--;
else break;
}
if(a==la[stkn] && b>=lb[stkn] && z!=0)
return;
la[++stkn]=a;
lb[stkn]=b;
lz[stkn]=z;
}
pli query(ll x){
while(pt<stkn && (lb[pt]-lb[pt+1])<(la[pt+1]-la[pt])*x) pt++;
return make_pair(la[pt]*x+lb[pt],lz[pt]);
}
}CHT;
ll a[MAXN],D[2][MAXN],S[MAXN];
int pre[205][MAXN];
int main(){
int n,k;
scanf("%d %d",&n,&k);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]), S[i]=S[i-1]+a[i];
for(int i=1;i<=k;i++){
CHT c; c.insert(0,0,0);
for(int j=1;j<=n;j++){
pli temp=c.query(S[j]);
D[i%2][j]=temp.first;
pre[i][j]=temp.second;
c.insert(S[j],D[(i-1)%2][j]-S[j]*S[j],j);
}
}
printf("%lld\n",D[k%2][n]);
int here=n;
vi ans;
ans.push_back(-1);
for(int i=1;i<=k;i++){
ans.push_back(pre[k-i+1][here]);
here=pre[k-i+1][here];
}
sort(ans.begin(),ans.end());
for(int i=1;i<=k;i++){
if(ans[i]==0) ans[i]=1;
if(ans[i]<=ans[i-1]) ans[i]=ans[i-1]+1;
printf("%d ",ans[i]);
}
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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |