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>
#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif
using namespace std;
int N, K, PS[100001], cnt[100001], P[100001];
long long D[100001];
double cross(int a, int b)
{
if(PS[a]==PS[b]) return 0;
return 1.*(D[b]-2LL*PS[b]*PS[b]-D[a]+2LL*PS[a]*PS[a])/(2LL*PS[a]-2LL*PS[b]);
}
pair<long long,int> solve(long long l)
{
vector<int> S(1);
int j=0;
for(int i=1;i<=N;i++) {
while(j+1<S.size() && cross(S[j],S[j+1])<PS[i]) j++;
D[i]=D[S[j]]+2LL*(PS[i]-PS[S[j]])*PS[S[j]]-l;
cnt[i]=cnt[S[j]]+1;
P[i]=S[j];
if(PS[i]==PS[S.back()] && D[i]<=D[S.back()]) continue;
while(S.size()>j+1) {
int c=S.back();
S.pop_back();
if(cross(S.back(),c)<=cross(S.back(),i)) {
S.push_back(c);
break;
}
}
S.push_back(i);
}
return {D[N],cnt[N]};
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
TEST(freopen("input.txt","r",stdin));
TEST(freopen("output.txt","w",stdout));
TEST(freopen("debug.txt","w",stderr));
long long s=0, e=1e18;
int j=1, k2;
vector<int> P1, P2, res(1);
cin>>N>>K; ++K;
for(int i=1;i<=N;i++) {
cin>>PS[i];
PS[i]+=PS[i-1];
}
while(s<=e) {
long long m=(s+e)>>1;
if(solve(2*m+1).second>K) s=m+1;
else e=m-1;
}
solve(2*s+1);
k2=cnt[N];
for(int c=N;c;c=P[c]) P2.push_back(c);
P2.push_back(0); reverse(P2.begin(),P2.end());
solve(2*e+1);
for(int c=N;c;c=P[c]) P1.push_back(c);
P1.push_back(0); reverse(P1.begin(),P1.end());
for(int i=1;i<P1.size();i++) {
while(j<P2.size() && P1[i-1]>=P2[j]) j++;
if(P1[i]<=P2[j] && i-j==K-k2) {
long long ans=0;
for(int k=1;k<i;k++) res.push_back(P1[k]);
for(int k=j;k+1<P2.size();k++) res.push_back(P2[k]);
for(int k=1;k<res.size();k++) ans+=1LL*(PS[N]-PS[res[k]])*(PS[res[k]]-PS[res[k-1]]);
cout<<ans<<'\n';
for(int k=1;k<res.size();k++) cout<<res[k]<<' ';
return 0;
}
}
return -1;
}
Compilation message (stderr)
sequence.cpp: In function 'std::pair<long long int, int> solve(long long int)':
sequence.cpp:27:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(j+1<S.size() && cross(S[j],S[j+1])<PS[i]) j++;
~~~^~~~~~~~~
sequence.cpp:32:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(S.size()>j+1) {
~~~~~~~~^~~~
sequence.cpp: In function 'int main()':
sequence.cpp:72:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<P1.size();i++) {
~^~~~~~~~~~
sequence.cpp:73:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(j<P2.size() && P1[i-1]>=P2[j]) j++;
~^~~~~~~~~~
sequence.cpp:77:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k=j;k+1<P2.size();k++) res.push_back(P2[k]);
~~~^~~~~~~~~~
sequence.cpp:78:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k=1;k<res.size();k++) ans+=1LL*(PS[N]-PS[res[k]])*(PS[res[k]]-PS[res[k-1]]);
~^~~~~~~~~~~
sequence.cpp:80:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k=1;k<res.size();k++) cout<<res[k]<<' ';
~^~~~~~~~~~~
# | 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... |