Submission #100106

#TimeUsernameProblemLanguageResultExecution timeMemory
100106ansol4328Split the sequence (APIO14_sequence)C++11
60 / 100
471 ms132096 KiB
#include<stdio.h> #include<deque> using namespace std; typedef long long ll; struct line { ll slope, constant, num; line() : slope(0), constant(0), num(0) {} line(ll a, ll b, ll 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; } ll get_line_num() { line L1=L.front(); return L1.num; } }; int n, k; ll sum[100005]; ll dp[205][100005], path[205][100005]; CHT cht[202]; 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++) { for(int j=1 ; j<=n ; j++) { ll val=cht[i-1].query(sum[j]); dp[i][j]=val; path[i][j]=cht[i-1].get_line_num(); cht[i].update(line(sum[j],dp[i][j]-sum[j]*sum[j],(ll)j)); } } printf("%lld\n",dp[k][n]); ll 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("%lld ",res[i]); return 0; }

Compilation message (stderr)

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