Submission #25175

#TimeUsernameProblemLanguageResultExecution timeMemory
25175RezwanArefin01Split the sequence (APIO14_sequence)C++14
71 / 100
73 ms34048 KiB
//Bismillahir Rahmanir Rahim #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 10001, K = 201; ll dp[K][N], par[K][N], arr[N], p[N]; struct CHT{ vector<ll> m, b, id; int ptr = 0; bool bad(int l1, int l2, int l3) { return (b[l3] - b[l1]) * (m[l1] - m[l2]) <= (b[l2] - b[l1]) * (m[l1] - m[l3]); } void clear() { ptr = 0; m.clear(); b.clear(); id.clear(); } void add(ll _m, ll _b, ll idx) { m.push_back(_m); b.push_back(_b); id.push_back(idx); int s = m.size(); while(s >= 3 && bad(s-3, s-2, s-1)) { s--; m.erase(m.end()-2); b.erase(b.end()-2); id.erase(id.end()-2); } } ll f(int i, ll x) { return m[i]*x + b[i]; } pair<ll,ll> query(ll x) { if(ptr >= m.size()) ptr = m.size()-1; while(ptr < m.size()-1 && f(ptr+1, x) >= f(ptr, x)) ptr++; return {id[ptr], f(ptr, x)}; } }; void recon(int k, int n) { if(!k) return; recon(k-1, par[k][n]); cout<<par[k][n]<<" "; } int main(int argc, char const *argv[]) { #ifdef LOCAL_TESTING freopen("in", "r", stdin); #endif int n, kk; cin>>n>>kk; for(int i=1; i <= n; i++) { cin>>arr[i]; p[i] = p[i-1] + arr[i]; } for(int i=0; i<=kk; i++) for(int j=0; j<=n; j++) dp[i][j] = -1e18; for(int i=0; i<=n; i++) dp[0][i] = 0; CHT ds; for(int k=1; k <= kk; k++) { ds.clear(); ds.add(0, 0, 0); for(int i=k; i<=n; i++) { auto it = ds.query(p[i]); dp[k][i] = it.second; ds.add(p[i], dp[k-1][i] - p[i] * p[i], i); par[k][i] = it.first; } } cout<<dp[kk][n]<<endl; recon(kk, n); }

Compilation message (stderr)

sequence.cpp: In member function 'std::pair<long long int, long long int> CHT::query(ll)':
sequence.cpp:35:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(ptr >= m.size()) ptr = m.size()-1;
          ^
sequence.cpp:36:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(ptr < m.size()-1 && 
             ^
#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...