이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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[K+1][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();
pointer=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>0;k--){
n = path[k][n];
ans.push_back(n);
}
reverse(all(ans));
for(auto x:ans) cout<<x<<" ";
return 0;
}
컴파일 시 표준 에러 (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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |