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<stdio.h>
#include<deque>
using namespace std;
typedef long long ll;
struct line
{
ll slope, constant, num;
line() : slope(0), constant(0), num(0) {}
line(ll a, ll b, ll c) : slope(a), constant(b), num(c) {}
};
struct CHT
{
int sz;
deque<line> L;
CHT() : sz(0) {}
double get_point(line L1, line L2)
{
return (double)(L2.constant-L1.constant)/(L1.slope-L2.slope);
}
bool increase(line L1, line L2, line L3)
{
double p1=get_point(L1,L2);
double p2=get_point(L2,L3);
return p1<=p2;
}
void update(line L3)
{
while(sz>1)
{
line L2=L.back();
L.pop_back();
sz--;
line L1=L.back();
if(increase(L1,L2,L3))
{
L.push_back(L2);
sz++;
break;
}
}
L.push_back(L3);
sz++;
}
ll query(ll x)
{
while(sz>1 && get_point(L[0],L[1])<(double)x)
{
L.pop_front();
sz--;
}
line L1=L.front();
return L1.slope*x+L1.constant;
}
ll get_line_num()
{
line L1=L.front();
return L1.num;
}
void clr()
{
while(!L.empty()) L.pop_back();
sz=0;
}
};
int n, k;
ll sum[100001];
ll dp[201][100001], path[201][100001];
CHT cht[2];
int main()
{
scanf("%d %d",&n,&k);
for(int i=1 ; i<=n ; i++)
{
scanf("%lld",&sum[i]);
sum[i]+=sum[i-1];
}
k++;
cht[0].update(line(0,0,0));
for(int i=1 ; i<=k ; i++)
{
int prev=(i-1)%2;
int cur=i%2;
cht[cur].clr();
for(int j=1 ; j<=n ; j++)
{
ll val=cht[prev].query(sum[j]);
dp[i][j]=val;
path[i][j]=cht[prev].get_line_num();
cht[cur].update(line(sum[j],dp[i][j]-sum[j]*sum[j],(ll)j));
}
}
printf("%lld\n",dp[k][n]);
ll res[205];
int cnt=k;
res[k]=n;
while(cnt!=1)
{
res[cnt-1]=path[cnt][res[cnt]];
cnt--;
}
for(int i=1 ; i<k ; i++) printf("%lld ",res[i]);
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:76:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&k);
~~~~~^~~~~~~~~~~~~~~
sequence.cpp:79:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",&sum[i]);
~~~~~^~~~~~~~~~~~~~~~
# | 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... |