Submission #301746

#TimeUsernameProblemLanguageResultExecution timeMemory
301746ansol4328Split the sequence (APIO14_sequence)C++14
100 / 100
1053 ms84560 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; struct line{ ll slope, constant; int num; line() : slope(0), constant(0), num(0) {} line(ll a, ll b, int c) : slope(a), constant(b), num(c) {} }; struct CHT{ int sz; deque<line> L; CHT() : sz(0) {} double get_point(line L1, line L2){ return (double)(L2.constant-L1.constant)/(L1.slope-L2.slope); } bool increase(line L1, line L2, line L3){ double p1=get_point(L1,L2); double p2=get_point(L2,L3); return p1<=p2; } void update(line L3){ while(sz>1){ line L2=L.back(); L.pop_back(); sz--; line L1=L.back(); if(increase(L1,L2,L3)){ L.push_back(L2); sz++; break; } } L.push_back(L3); sz++; } ll query(ll x){ while(sz>1 && get_point(L[0],L[1])<(double)x){ L.pop_front(); sz--; } line L1=L.front(); return L1.slope*x+L1.constant; } int get_line_num(){ line L1=L.front(); return L1.num; } void clr(){ while(!L.empty()) L.pop_back(); sz=0; } }; int n, k; ll sum[100005]; ll dp[100005]; int path[205][100005]; CHT cht[2]; int main() { scanf("%d %d",&n,&k); for(int i=1 ; i<=n ; i++){ scanf("%lld",&sum[i]); sum[i]+=sum[i-1]; } k++; cht[0].update(line(0,0,0)); for(int i=1 ; i<=k ; i++){ int prev=(i-1)%2; int cur=i%2; cht[cur].clr(); for(int j=i ; j<=n ; j++){ ll val=cht[prev].query(sum[j]); dp[j]=val+sum[n]*sum[j]-sum[j]*sum[j]; path[i][j]=cht[prev].get_line_num(); cht[cur].update(line(sum[j],dp[j]-sum[n]*sum[j],j)); } } printf("%lld\n",dp[n]); int res[205]; int cnt=k; res[k]=n; while(cnt!=1){ res[cnt-1]=path[cnt][res[cnt]]; cnt--; } for(int i=1 ; i<k ; i++) printf("%d ",res[i]); return 0; }

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:66:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   66 |     scanf("%d %d",&n,&k);
      |     ~~~~~^~~~~~~~~~~~~~~
sequence.cpp:68:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   68 |         scanf("%lld",&sum[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~
#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...