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;
typedef pair<long long,int> pll;
long long s[100100],dp[100100][2];
int n,k,rev[100100][210];
class line
{
public:
long long a,b;
bool operator == (const line& rhs) const
{
return (a==rhs.a)&&(b==rhs.b);
}
};
double cross(line l1, line l2)
{
return (double) (l1.b-l2.b)/(l2.a-l1.a);
}
class convexhulltrick
{
public:
int s,e,ind[100100];
line deq[100100];
void insert(line l,int in)
{
if(deq[e-1]==l)
{
ind[e-1] = in;
return;
}
ind[e] = in;
deq[e] = l;
while(s+1<e&&cross(deq[e-2],deq[e-1])>=cross(deq[e-1],deq[e]))
{
deq[e-1] = deq[e];
ind[e-1] = ind[e];
e--;
}
e++;
}
pll query(long long x)
{
long long l = s,r = e-1;
while(l!=r)
{
long long m = (l+r)/2;
if(cross(deq[m],deq[m+1])<=x)
l = m+1;
else r = m;
}
return {deq[l].a*x+deq[l].b,ind[l]};
}
void init()
{
s=e=0;
}
};
convexhulltrick cht;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>s[i];
for(int i=1;i<=n;i++)
s[i] += s[i-1];
for(int i=0;i<k;i++)
{
cht.init();
int ind = (i+1)%2;
dp[0][ind] = -1000000000000;
line tmp;
tmp.a = 0;
tmp.b = -1000000000000;
cht.insert(tmp,0);
for(int j=1;j<=n;j++)
{
pll hamin = cht.query(s[j]);
dp[j][ind] = hamin.first;
rev[j][i] = hamin.second;
line tmp;
tmp.a = s[j];
tmp.b = dp[j][1-ind]-s[j]*s[j];
cht.insert(tmp,j);
}
}
long long last = n;
cout<<dp[n][k%2]<<"\n";
for(int i=0;i<k;i++)
{
last = rev[last][k-i-1];
cout<<last<<" ";
}
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:26:23: warning: array subscript is below array bounds [-Warray-bounds]
if(deq[e-1]==l)
~~~~~~~^
sequence.cpp:26:23: warning: array subscript is below array bounds [-Warray-bounds]
# | 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... |