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>
using namespace std;
long long psum[110001];
int n;
struct Line {
long long a,b;
};
struct Fraction {
long long up,down;
};
Fraction cross(Line one,Line two) {
if (one.a<two.a) {
swap(one,two);
}
return {two.b-one.b,one.a-two.a};
}
bool cmp(Fraction one,Fraction two) {
return one.up*two.down<one.down*two.up;
}
Line arr[110001];
int sz;
void insert(Line l) {
if (sz>=1&&arr[sz-1].a==l.a) {
if (l.b>arr[sz-1].b) {
arr[sz-1]=l;
}
return;
}
while (sz>1) {
Fraction before=cross(arr[sz-2],arr[sz-1]);
Fraction now=cross(arr[sz-1],l);
if (cmp(before,now)) {
break;
}
sz--;
}
arr[sz++]=l;
}
vector<int> ret;
long long before[110005];
long long before2[110005];
long long now[110005];
long long getans(int x1,int y1,int x2,int y2) {
int ind=0;
if (y1+1==y2) {
ret.push_back(x2);
return 0;
}
int mid=(y1+y2)/2;
for(int i=x1;i<=x2;i++) {
before[i]=-1e18;
before2[i]=-1e18;
}
before[x1]=0;
before2[x2]=0;
for(int i=y1+1;i<=mid;i++) {
sz=0;
ind=0;
for(int j=x1;j<=x2;j++) {
while (ind+1<sz&&cmp(cross(arr[ind],arr[ind+1]),{psum[j],1})) {
ind++;
}
long long val;
if (ind>=sz) {
val=-1e18;
}
else {
val=arr[ind].a*psum[j]+arr[ind].b;
}
now[j]=val;
if (before[j]>=0)
insert({psum[j],-psum[j]*psum[j]+before[j]});
}
for(int j=x1;j<=x2;j++) {
before[j]=now[j];
}
}
for(int i=y2-1;i>=mid;i--) {
sz=0;
ind=0;
for(int j=x2;j>=x1;j--) {
while (ind+1<sz&&cmp(cross(arr[ind],arr[ind+1]),{-psum[j],1})) {
ind++;
}
long long val;
if (ind>=sz) {
val=-1e18;
}
else {
val=arr[ind].a*(-psum[j])+arr[ind].b-psum[j]*psum[j];
}
now[j]=val;
if (before2[j]>=0)
insert({-psum[j],before2[j]});
}
for(int j=x1;j<=x2;j++) {
before2[j]=now[j];
}
}
int opt=-1;
long long maxi=-1e18;
for(int i=x1;i<=x2;i++) {
if (before[i]+before2[i]>maxi) {
maxi=before[i]+before2[i];
opt=i;
}
}
getans(x1,y1,opt,mid);
getans(opt,mid,x2,y2);
return maxi;
}
int main(void) {
int k;
scanf("%d %d\n",&n,&k);
for(int i=0;i<n;i++) {
long long x;
scanf("%lld",&x);
psum[i+1]=psum[i]+x;
}
printf("%lld\n",getans(0,0,n,k+1));
for(int i=0;i<k;i++) {
printf("%d ",ret[i]);
}
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:124:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d\n",&n,&k);
~~~~~^~~~~~~~~~~~~~~~~
sequence.cpp:127:14: 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... |