이 제출은 이전 버전의 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;
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(int m,int c,int l){
if(!M.empty() && M.back() == m) {
if(c>=C.back()){
C[C.size()-1] = c;
L[L.size()-1] = l;
}
return;
}
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;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'std::pair<long long int, long long int> query(long long int)':
sequence.cpp:44: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 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... |