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;
#define ll long long
#define mp make_pair
const int N=100050;
struct Line{ ll k,n;int id;Line(ll a=0, ll b=0, int c=0):k(a),n(b),id(c){}pair<ll,int> Get(ll x){ return mp(k*x+n,id);}} Hull[N];
ll sec(Line a, Line b)
{
ll p=a.n-b.n,q=b.k-a.k;
if(q<0) p=-p,q=-q;
if(p>0) return (p+q-1)/q;
else return p/q;
}
int hsz,ptr=1;
void AddLine(Line l){ while((hsz>=ptr && Hull[hsz].k==l.k && Hull[hsz].n<l.n) || (Hull[hsz].k!=l.k && hsz>ptr && sec(Hull[hsz-1],Hull[hsz])>=sec(Hull[hsz],l))) hsz--;if(!(hsz>=ptr && Hull[hsz].k==l.k)) Hull[++hsz]=l;}
pair<ll,int> Get(ll x){ while(ptr<hsz && Hull[ptr].Get(x)<Hull[ptr+1].Get(x)) ptr++;return Hull[ptr].Get(x);}
ll dp[N],sum[N];
int go[N],cnt[N];
void Solve(int n, ll C)
{
hsz=0;ptr=1;
AddLine(Line(0,0,0));
for(int i=1;i<=n;i++)
{
pair<ll,int> ans=Get(sum[i]);
go[i]=ans.second;
dp[i]=ans.first-C;
cnt[i]=cnt[go[i]]+1;
AddLine(Line(sum[i],dp[i]-sum[i]*sum[i],i));
}
}
int main()
{
int n,k;
scanf("%i %i",&n,&k);k++;
for(int i=1;i<=n;i++) scanf("%lld",&sum[i]),sum[i]+=sum[i-1];
ll top=1e15,bot=0,mid;
while(top>=bot)
{
mid=top+bot>>1;
Solve(n,mid);
if(cnt[n]==k) break;
if(cnt[n]<k) top=mid-1;
else bot=mid+1;
}
ll ans=dp[n]+mid*k;
vector<int> pos;
for(int i=n;i;i=go[i]) pos.push_back(i);
reverse(pos.begin(),pos.end());
pos.pop_back();
printf("%lld\n",ans);
for(int i:pos) printf("%i ",i);
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:40:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
mid=top+bot>>1;
~~~^~~~
sequence.cpp:35:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&n,&k);k++;
~~~~~^~~~~~~~~~~~~~~
sequence.cpp:36:45: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=n;i++) scanf("%lld",&sum[i]),sum[i]+=sum[i-1];
~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
# | 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... |