Submission #173131

#TimeUsernameProblemLanguageResultExecution timeMemory
173131gs18081Split the sequence (APIO14_sequence)C++11
62 / 100
1189 ms88652 KiB
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll,ll> pl; struct line{ pl l; int idx; line(){} line(pl temp,int i){l = temp;idx = i;} }; const int MAXN = 101010; const int MAXK = 220; int N,K; ll DT[2][MAXN]; int from[MAXK][MAXN]; int arr[MAXN]; ll sum[MAXN]; ll calc(line l,ll p){ return l.l.first * p + l.l.second; } int main(){ scanf("%d %d",&N,&K); for(int i=1;i<=N;i++){ scanf("%d",arr+i); sum[i] = sum[i-1] + arr[i]; } for(int j=1;j<=K+1;j++){ int now = 0; int p = (j+1) & 1; int n = j & 1; vector<line> hull; pl temp = pl(sum[j-1] , DT[p][j-1] - sum[j-1] * sum[N]); hull.push_back(line(temp,j-1)); for(int i=j;i<=N;i++){ while(now < hull.size() - 1 && calc(hull[now] , sum[i] ) <= calc(hull[now+1],sum[i])) now++; DT[n][i] = calc(hull[now],sum[i]) + sum[N] * sum[i] - sum[i] * sum[i]; from[j][i] = hull[now].idx; // printf("%d %d %d %d %d\n",j,i,from[j][i],now,hull.size()); pl temp = pl(sum[i] , DT[p][i] - sum[i] * sum[N]); while(!hull.empty() && hull.back().l.second <= temp.second){ hull.pop_back(); } if(hull.empty() || hull.back().l.first < temp.first) hull.push_back(line(temp,i)); if(now >= hull.size()) now = hull.size() - 1; } } printf("%lld\n",DT[(K+1) & 1][N]); int now = N; int h = K+1; now = from[h][now]; h--; vector<int> ans; while(h!=0){ ans.push_back(now); assert(1<= now && now <= N-1); now = from[h][now]; h--; } while(!ans.empty()){ printf("%d ",ans.back()); ans.pop_back(); } printf("\n"); return 0; }

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:45:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    while(now < hull.size() - 1 && calc(hull[now] , sum[i] ) <= calc(hull[now+1],sum[i])) now++;
          ~~~~^~~~~~~~~~~~~~~~~
sequence.cpp:54:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    if(now >= hull.size()) now = hull.size() - 1;
       ~~~~^~~~~~~~~~~~~~
sequence.cpp:32:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&N,&K);
  ~~~~~^~~~~~~~~~~~~~~
sequence.cpp:34:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",arr+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...