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 long long ll;
struct CHT{
ll s[100005], t[100005];
int id[100005];
int st, ed, sz;
CHT(){
st=0, ed=-1, sz=0;
}
bool ck(ll L1s, ll L1t, ll L2s, ll L2t, ll mes, ll met){
///x1 = (L1.t-L2.t) /(L2.s-L1.s)
///x2 = (L1.t - me.t) / (me.s-L1.s)
///x1<x2
return (L1t-L2t) * (mes-L1s) < (L1t-met) * (L2s-L1s) ;
}
void init(){
st=0, ed=-1, sz=0;
}
void push(ll ss, ll tt, int idx){
while(sz>=2 && !ck(s[ed-1], t[ed-1], s[ed], t[ed], ss, tt)){
--ed;--sz;
}
if(sz!=0 && s[ed] == ss){
if(t[ed]>=tt)return;
s[ed] = ss;t[ed] = tt;id[ed] = idx;
}
else{
++sz;++ed;
s[ed] = ss; t[ed] = tt; id[ed] = idx;
}
}
ll val(int ii, ll x)
{
return s[ii] * x + t[ii];
}
void Efront(ll x)
{
while(sz>=2 && !(val(st, x)>val(st+1, x))){
// if(que[1].idx == id)return;
--sz;++st;
}
}
ll maxval(ll x){
return val(st, x);
}
}cht;
int n, k, A[100005];
ll s[100005], dp[100005][2];
int pre[100003][202];
void trace(int now, int j)
{
vector<int> tmp;
while(1){
if(j==0)break;
tmp.push_back(now);
now = pre[now][j];--j;
}
for(int i=(int)tmp.size()-1; i>=0; i--)
printf("%d ", tmp[i]);
}
int main()
{
scanf("%d %d", &n, &k);
for(int i=1; i<=n; i++){
scanf("%d", &A[i]);
}
for(int i=1; i<=n; i++){
s[i] = s[i-1] + A[i];
}
for(int j=1; j<=k; j++){
cht.init();
int pos = 0;
int t = (j&1);
for(int i=j; i<=n; i++){
/// cht.Efront(s[i]);
cht.push(s[i-1], -s[i-1]*s[n] + dp[i-1][!t], i-1);
cht.Efront(s[i]);
dp[i][t] = cht.maxval(s[i]) + s[i]*(s[n]-s[i]);
pre[i][j] = cht.id[cht.st];
}
}
ll ans=-1, ed=-1;
for(int i=k; i<=n; i++){
if(ans<dp[i][k&1]){
ans = dp[i][k&1];ed=i;
}
}
cout<<ans<<endl;
trace(ed, k);
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:78:13: warning: unused variable 'pos' [-Wunused-variable]
int pos = 0;
^
sequence.cpp:67:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k);
^
sequence.cpp:69:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &A[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... |