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>
#include<inttypes.h>
#define MAX_N 100005
#define MAX_K 205
#define pb push_back
#define sq(x) ((x)*(x))
#define abs(x) ((x)>0?(x):(-(x)))
#define LL_inf (1LL<<60)
#define full_inf 0x7fffffff
#define half_inf 0x3fffffff
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pil;
typedef long double LD;
struct node{
LL fi,se,from;
};
LL n,k,l,r,ans,ansi,anss[MAX_K];
LL sum[MAX_N],t[3][MAX_N];
int From[MAX_K][MAX_N];
node line[MAX_N];
bool is_ok(node p1,node p2,node p3){return ((p1.fi-p2.fi)*(p3.se-p1.se))<=((p1.fi-p3.fi)*(p2.se-p1.se));}
int main(){
LL i,j,x,y;
node p;
scanf("%lld %lld",&n,&k);
for(i=1;i<=n;i++){
scanf("%lld",&x);
sum[i]=sum[i-1]+x;
}
for(i=1;i<n;i++) t[1][i]=sum[i]*(sum[n]-sum[i]);
ans=-LL_inf;
if(k==1){
for(i=1;i<n;i++){
if(ans<t[1][i]){
ansi=i;
ans=t[1][i];
}
}
return !printf("%lld\n%lld\n",ans,ansi);
}
for(i=2;i<=k;i++){
l=r=0;
line[r++]={sum[i-1],t[1-(i%2)][i-1],i-1};
for(j=i;j<=n;j++){
x=sum[j]-sum[n];
t[i%2][j]=-LL_inf;
while(r-l>1 && line[l].fi*x+line[l].se<=line[l+1].fi*x+line[l+1].se) l++;
if(r-l){
y=line[l].fi*x+line[l].se+sum[j]*(-x);
if(t[i%2][j]<y){
t[i%2][j]=y;
From[i][j]=line[l].from;
}
}
y=t[1-(i%2)][j-1]+(sum[j]-sum[j-1])*(-x);
if(t[i%2][j]<y){
t[i%2][j]=y;
From[i][j]=j-1;
}
p={sum[j],t[1-(i%2)][j],j};
while(r-l>1 && is_ok(line[r-2],line[r-1],p)) r--;
line[r++]=p;
if(i==k && ans<t[i%2][j]){ans=t[i%2][j];ansi=j;}
}
}
printf("%lld\n",ans);
i=k;
while(i){
anss[i]=ansi;
ansi=From[i][ansi];
i--;
}
for(i=1;i<=k;i++) printf("%lld ",anss[i]);
printf("\n");
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:27:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld",&n,&k);
~~~~~^~~~~~~~~~~~~~~~~~~
sequence.cpp:29:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",&x);
~~~~~^~~~~~~~~~~
# | 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... |