Submission #551574

#TimeUsernameProblemLanguageResultExecution timeMemory
551574ToroTNSplit the sequence (APIO14_sequence)C++14
100 / 100
647 ms87716 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define X first #define Y second struct line { ll m,c; }; ll n,m,dp[100005][2],a[100005],qs[100005],u,g; ll sum[100005],pos,cut; int back[100005][205]; line t,now,pre; stack<ll> stk; vector<pair<line,ll> > v; ll cal(line node,ll ptr) { return node.m*sum[ptr]+node.c; } ll intersect(line l1,line l2,line now) { return (now.c-l1.c)*(l2.m-now.m)-(now.c-l2.c)*(l1.m-now.m)<=0; } int main() { scanf("%lld%lld",&n,&m); for(int i=1;i<=n;i++)scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i],qs[i]=qs[i-1]+a[i]*a[i]; for(int i=0;i<=n;i++)for(int j=0;j<=1;j++)dp[i][j]=2e18; dp[0][0]=0; memset(back,-1,sizeof back); for(int i=1;i<=m+1;i++) { for(int j=0;j<=n;j++)dp[j][i%2]=2e18; for(int j=0;j<=n;j++) { if(pos>=v.size())pos=v.size()-1; while(pos+1<v.size()&&cal(v[pos+1].X,j)<=cal(v[pos].X,j))pos++; if(v.size()>0) { dp[j][i%2]=cal(v[pos].X,j)+sum[j]*sum[j]-qs[j]; back[j][i]=v[pos].Y; } if(j<i-1)continue; t={-2*sum[j],dp[j][1-(i%2)]+(sum[j]*sum[j]+qs[j])}; while(v.size()>=1) { now=v[v.size()-1].X; if(now.m==t.m&&t.c<=now.c) { v.pop_back(); }else { break; } } while(v.size()>=2) { now=v[v.size()-1].X; pre=v[v.size()-2].X; cut=intersect(now,pre,t); if(cut) { v.pop_back(); }else { break; } } v.pb({t,j}); //printf("%d %d %lld\n",i,j,dp[j][i%2]); } while(!v.empty())v.pop_back(); } u=n; g=m+1; while(1) { u=back[u][g]; --g; if(u==0)break; stk.push(u); } printf("%lld\n",(sum[n]*sum[n]-qs[n]-dp[n][(m+1)%2])/2); while(!stk.empty()) { printf("%lld ",stk.top()); stk.pop(); } printf("\n"); } /* 7 3 4 1 3 4 0 2 3 */

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:37:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<line, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |             if(pos>=v.size())pos=v.size()-1;
      |                ~~~^~~~~~~~~~
sequence.cpp:38:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<line, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |             while(pos+1<v.size()&&cal(v[pos+1].X,j)<=cal(v[pos].X,j))pos++;
      |                   ~~~~~^~~~~~~~~
sequence.cpp:27:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     scanf("%lld%lld",&n,&m);
      |     ~~~~~^~~~~~~~~~~~~~~~~~
sequence.cpp:28:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |     for(int i=1;i<=n;i++)scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i],qs[i]=qs[i-1]+a[i]*a[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...