#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;
}
Compilation message
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;
~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2680 KB |
Output is correct |
2 |
Correct |
3 ms |
2680 KB |
Output is correct |
3 |
Correct |
3 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
3 ms |
2680 KB |
Output is correct |
6 |
Runtime error |
8 ms |
5112 KB |
Execution killed with signal 8 (could be triggered by violating memory limits) |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2684 KB |
Output is correct |
2 |
Correct |
3 ms |
2680 KB |
Output is correct |
3 |
Correct |
3 ms |
2684 KB |
Output is correct |
4 |
Correct |
3 ms |
2652 KB |
Output is correct |
5 |
Correct |
4 ms |
2808 KB |
Output is correct |
6 |
Runtime error |
8 ms |
5112 KB |
Execution killed with signal 8 (could be triggered by violating memory limits) |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
2808 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
11512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |