Submission #247039

#TimeUsernameProblemLanguageResultExecution timeMemory
247039hackermubSplit the sequence (APIO14_sequence)C++17
100 / 100
665 ms84400 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long #define pii pair<ll,ll> #define fi first #define se second #define all(v) v.begin(),v.end() #define pb push_back const int MOD =1e9+7; const int64_t INF = 1e18; vector<ll> M,C; vector<int> L; int last; bool bad(int l1,int l2,int l3){ return __int128(C[l3]-C[l1])*(M[l1]-M[l2]) < __int128(C[l2]-C[l1])*(M[l1]-M[l3]); } void insert(ll m,ll c,int l){ if(!M.empty() && M.back() == m) { if(c>=C.back()){ M.pop_back(); C.pop_back(); L.pop_back(); } } M.push_back(m); C.push_back(c); L.push_back(l); while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)){ M.erase(M.end()-2); C.erase(C.end()-2); L.erase(L.end()-2); } } ll f(int idx,ll x){ return M[idx]*x + C[idx]; } pii query(ll x){ last = min(last , (int)M.size()-1); while(last<M.size()-1 && f(last,x)<=f(last+1,x)) last++; return {f(last,x),L[last]}; } int32_t main(){ ios_base::sync_with_stdio(false);cin.tie(); //If you hack my code , You are gay; int n,K; cin>>n>>K; vector<ll> pre(n+1); for(int i=1;i<=n;i++){ cin>>pre[i]; pre[i]+=pre[i-1]; } ll dp[2][n+1]; int path[K+1][n+1]; memset(dp,0,sizeof(dp)); memset(path,0,sizeof(path)); for(int k=1;k<=K;k++){ M.clear(); C.clear(); L.clear(); last=0; insert(0,0,0); for(int i=1;i<=n;i++){ pii now = query(pre[i]); dp[k%2][i] = now.fi; path[k][i] = now.se; insert(pre[i],dp[!(k%2)][i]-pre[i]*pre[i],i); } } cout<<dp[K%2][n]<<"\n"; vector<int> ans; for(int k=K;k;k--){ n = path[k][n]; ans.push_back(n); } reverse(all(ans)); for(auto x:ans) cout<<x<<" "; return 0; }

Compilation message (stderr)

sequence.cpp: In function 'std::pair<long long int, long long int> query(long long int)':
sequence.cpp:45:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(last<M.size()-1 && f(last,x)<=f(last+1,x)) last++;
           ~~~~^~~~~~~~~~~
#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...