Submission #224566

#TimeUsernameProblemLanguageResultExecution timeMemory
224566kig9981Split the sequence (APIO14_sequence)C++17
0 / 100
180 ms3900 KiB
#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 -1; return 1.*(D[b]-1LL*PS[b]*PS[b]-D[a]+1LL*PS[a]*PS[a])/(PS[a]-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() && D[S[j]]+1LL*(PS[i]-PS[S[j]])*PS[S[j]]<D[S[j+1]]+1LL*(PS[i]-PS[S[j+1]])*PS[S[j+1]]) j++; D[i]=D[S[j]]+1LL*(PS[i]-PS[S[j]])*PS[S[j]]-l; cnt[i]=cnt[S[j]]+1; P[i]=S[j]; while(S.size()>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(m).second>K) s=m+1; else e=m-1; } solve(s); if(cnt[N]==K) { cout<<D[N]+K*s<<'\n'; for(int c=P[N];c;c=P[c]) P1.push_back(c); reverse(P1.begin(),P1.end()); for(auto r: P1) cout<<r<<' '; return 0; } 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(e); 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() && D[S[j]]+1LL*(PS[i]-PS[S[j]])*PS[S[j]]<D[S[j+1]]+1LL*(PS[i]-PS[S[j+1]])*PS[S[j+1]]) j++;
         ~~~^~~~~~~~~
sequence.cpp: In function 'int main()':
sequence.cpp:78:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=1;i<P1.size();i++) {
              ~^~~~~~~~~~
sequence.cpp:79:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(j<P2.size() && P1[i-1]>=P2[j]) j++;
         ~^~~~~~~~~~
sequence.cpp:83: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:84: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:86:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int k=1;k<res.size();k++) cout<<res[k]<<' ';
                ~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...