Submission #246741

#TimeUsernameProblemLanguageResultExecution timeMemory
246741hackermubSplit the sequence (APIO14_sequence)C++17
33 / 100
76 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; 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]; } int pointer; pii query(int x){ if(pointer>=M.size()) pointer=M.size()-1; while(pointer<M.size()-1 && f(pointer,x)<=f(pointer+1,x)) pointer++; return {f(pointer,x),L[pointer]}; } 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); int dp[2][n+1],path[K+1][n+1]; memset(dp,0,sizeof(dp)); memset(path,0,sizeof(path)); for(int i=1;i<=n;i++){ cin>>pre[i]; pre[i]+=pre[i-1]; //dp[0][i]=pre[i]; } for(int k=1;k<=K;k++){ M.clear(),C.clear(),L.clear(); pointer=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>0;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:36:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(pointer>=M.size()) pointer=M.size()-1;
        ~~~~~~~^~~~~~~~~~
sequence.cpp:37:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(pointer<M.size()-1 && f(pointer,x)<=f(pointer+1,x)) pointer++;
           ~~~~~~~^~~~~~~~~~~
#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...