이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
const int mn = 100009, mk = 109;
int f[mn][mk], n, k;
int l[mn], r[mn], a[mn];
vector<int> v[mn];
int dp[mn][mk];
void upd(int i, int j, int d){
i = n + 1 - i;
while(i <= n + 1){
f[i][j] = min(f[i][j], d);
i += (i & -i);
}
}
int cnt(int i, int j){
i = n + 1 - i;
int ans = 1e9;
while(i){
ans = min(ans, f[i][j]);
i -= (i & -i);
}
return ans;
}
main(){
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
stack<int> S;
for(int i = 1; i <= n; i++){
while(S.size() && a[S.top()] < a[i])
S.pop();
if(!S.size()) l[i] = 1;
else l[i] = S.top() + 1;
S.push(i);
}
while(S.size()) S.pop();
for(int i = n; i >= 1; i--){
while(S.size() && a[S.top()] < a[i])
S.pop();
if(!S.size()) r[i] = n;
else r[i] = S.top() - 1;
S.push(i);
}
for(int i = 1; i <= n; i++){
v[l[i]].push_back(i);
}
for(int i = 0; i <= n; i++){
for(int j = 0; j <= k; j++){
dp[i][j] = 1e9;
f[i+1][j] = 1e9;
}
}
dp[0][0] = 0;
upd(0, 0, 0);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= k; j++){
int s = cnt(i - 1, j - 1);
for(int x : v[i]){
dp[r[x]][j] = min(dp[r[x]][j],
a[x] + s);
upd(r[x], j, dp[r[x]][j]);
}
}
}
if(dp[n][k] == 1e9) cout <<1/0;
cout << dp[n][k] << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
blocks.cpp:24:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
blocks.cpp: In function 'int main()':
blocks.cpp:68:33: warning: division by zero [-Wdiv-by-zero]
if(dp[n][k] == 1e9) cout <<1/0;
~^~
# | 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... |