Submission #211696

#TimeUsernameProblemLanguageResultExecution timeMemory
211696LawlietSplit the sequence (APIO14_sequence)C++14
100 / 100
801 ms88320 KiB
#include <bits/stdc++.h> using namespace std; typedef long long int lli; typedef pair<int,int> pii; const int MAXK = 210; const int MAXN = 100010; const lli INF = 1000000000000000000LL; class ConvexHullTrick { public: bool canRemove(lli a, lli b, lli c, lli d) { if( b == 0 ) return true; if( a/b != c/d ) return a/b < c/d; a %= b; c %= d; if( a == 0 ) return true; if( c == 0 ) return false; return canRemove( d , c , b , a ); } void add(lli a, lli b, int i) { if( !A.empty() && A.back() == a && B.back() >= b ) return; while( sz > 1 && canRemove(B[sz] - b , a - A[sz] , B[sz - 1] - B[sz] , A[sz] - A[sz - 1]) ) { A.pop_back(); B.pop_back(); ind.pop_back(); sz--; } sz++; A.push_back( a ); B.push_back( b ); ind.push_back( i ); } lli getFunction(int i, lli x) { return A[i]*x + B[i]; } lli query(int x) { if( opt > sz ) opt = sz; while( opt < sz && getFunction(opt , x) <= getFunction(opt + 1 , x) ) opt++; return ind[opt]; } void clear() { A.clear(); B.clear(); ind.clear(); sz = -1; opt = 0; } ConvexHullTrick() : sz( -1 ), opt( 0 ) {} private: int sz; int opt; vector<lli> A; vector<lli> B; vector<int> ind; }; int n, k; int v[MAXN]; int opt[MAXN][MAXK]; lli sv[MAXN]; lli dp[MAXN][2]; ConvexHullTrick CHT; int main() { scanf("%d %d",&n,&k); for(int i = 1 ; i <= n ; i++) scanf("%d",&v[i]); for(int i = 1 ; i <= n ; i++) sv[i] = sv[i - 1] + v[i]; for(int j = 1 ; j <= k ; j++) { CHT.add( sv[j] , dp[j][1 - j%2] - sv[j]*sv[j] , j ); for(int i = j + 1 ; i <= n ; i++) { int& l = opt[i][j]; opt[i][j] = CHT.query( sv[i] ); dp[i][j%2] = dp[ l ][1 - j%2] - sv[l]*sv[l] + sv[l]*sv[i]; CHT.add( sv[i] , dp[i][1 - j%2] - sv[i]*sv[i] , i ); } CHT.clear(); } printf("%lld\n",dp[n][k%2]); int cur = n; for(int i = k ; i > 0 ; i--) { printf("%d ",opt[cur][i]); cur = opt[cur][i]; } } //dp(i,k) = dp(j - 1 , k) + sv[j - 1]*(sv[i] - sv[j - 1]) = dp(j - 1 , k) - (sv[j - 1]*sv[j - 1]) + (sv[j - 1]*sv[i])

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:95: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:98:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&v[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...