Submission #247023

#TimeUsernameProblemLanguageResultExecution timeMemory
247023hackermubSplit the sequence (APIO14_sequence)C++17
33 / 100
93 ms131076 KiB
#include<bits/stdc++.h> using namespace std; #define int long long #define pii pair<int,int> #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<int> M,C,L; int last; bool bad(int l1,int l2,int l3){ return (C[l3]-C[l1])*(M[l1]-M[l2]) < (C[l2]-C[l1])*(M[l1]-M[l3]); } void insert(int m,int c,int l){ 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); } } int f(int idx,int x){ return M[idx]*x + C[idx]; } pii query(int 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<int> pre(n+1); for(int i=1;i<=n;i++){ cin>>pre[i]; pre[i]+=pre[i-1]; } int dp[K+1][n+1],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][i] = now.fi; path[k][i] = now.se; insert(pre[i],dp[k-1][i]-pre[i]*pre[i],i); } } cout<<dp[K][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:37: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...