# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
81554 | kimjg1119 | Split the sequence (APIO14_sequence) | C++17 | 490 ms | 85544 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;
}
// (lb[stkn]-b)/(a-la[stkn]) <(lb[stkn-1]-lb[stkn])/(la[stkn]-la[stkn-1]) 일 때 stkn--; 인데
// 기울기가 일정하게 되면 a-la[stkn] 또는 la[stkn]-la[stkn-1]이 0이 될 수 있음.
// la[stkn]==la[stkn-1] 이거나 a==la[stkn]인 상황이 오면 교점이 존재하지 않음.
// la[stkn]==la[stkn-1] 인 상황이 없다고 가정하자. 그러면 a==la[stkn]인 상황만 처리해주면 됨.
// 두 직선의 기울기가 같으면 y절편이 큰 직선을 남기는 게 이득이라는 것이 자명함.
if(a==la[stkn] && b>=lb[stkn] && z!=0){
//lz[stkn]=z;
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]);
}
/*printf("\n");
for(int i=1;i<=k;i++)
for(int j=1;j<=n;j++)
printf("%d%c",pre[i][j]," \n"[j==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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |