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>
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define MAXN 100005
#define MAXK 205
typedef long long ll;
using namespace std;
ll n,u;
ll dp[MAXN][2];
ll niz[MAXN];
ll suma[MAXN];
ll sec[MAXN][MAXK];
pair<ll,ll> prava[MAXN];
vector<pair<pair<ll,ll>,ll> > prave;
int main()
{
scanf("%lld %lld", &n, &u);
for(ll i=1;i<=n;i++)
scanf("%lld",niz+i);
for(ll i=1;i<=n;i++)
suma[i]=suma[i-1]+niz[i];
for(ll k=1;k<=u;k++){
prave.clear();
for(int i=1;i<=n;i++)dp[i][0]=dp[i][1];
for(ll d=k+1;d<=n;d++){
pair<ll,ll> t=mp(suma[d-1],-suma[d-1]*suma[d-1]+dp[d-1][0]);
prava[d-1]=t;
while(!prave.empty()){
pair<ll,ll> posl=prava[prave.back().S];
if(posl.F==t.F){
prave.pop_back();
continue;
}
ll presek=(posl.S-t.S)/(t.F-posl.F)+((posl.S-t.S)%(posl.F-t.F)==0?0:1);
if(presek>10000000000000000ll)exit(2);
if(presek<=prave.back().F.F){
prave.pop_back();
continue;
}
prave.back().F.S=presek-1;
prave.pb(mp(mp(presek,10000000000000000ll),d-1));
break;
}
if(prave.empty()){
prave.pb(mp(mp(0ll,10000000000000000ll),d-1));
}
ll l=0,r=prave.size()-1,sol=-1;
while(l<=r){
ll mid=(l+r)/2;
if(prave[mid].F.F<=suma[d]){
sol=prave[mid].S;
l=mid+1;
}else
r=mid-1;
}
dp[d][1]=prava[sol].F*suma[d]+prava[sol].S;
sec[d][k]=sol;
/**
dp[d][k]=-1;
for(ll i=k+1;i<=d;i++){
ll tr=suma[i-1]*(suma[d]-suma[i-1])+dp[i-1][k-1];
if(tr>dp[d][k]){
sec[d][k]=i-1;
dp[d][k]=tr;
}
}
*/
//printf(" %lld %lld %lld\n",d,k,dp[d][k]);
/*for(auto x:prave){
printf(" %lld %lld %lld %lld\n",x.F.F,x.F.S,prava[x.S][k-1].F,prava[x.S][k-1].S);
}
printf("\n");*/
}
}
printf("%lld\n",dp[n][1]);
ll poc=n;
for(ll i=u;i>0;i--){
poc=sec[poc][i];
printf("%lld ",poc);
}
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:24:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld", &n, &u);
~~~~~^~~~~~~~~~~~~~~~~~~~~
sequence.cpp:27:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",niz+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... |