#include <bits/stdc++.h>
#define int long long
#define f first
#define pii pair<int, int>
#define s second
const int N = 1e5 + 5;
const int block = 450;
const int oo = 99999989;
const int base = 31;
using namespace std;
int n, a[N], dp[N][105], rmq[N][20], k, L[N];
stack <int> st;
void build(int k){
for(int i = 0;i <= n;i++) rmq[i][0] = dp[i][k];
for(int j = 1;(1 << j) <= n;j++){
for(int i = 0; i + (1 << j) - 1 <= n;i++){
rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
}
}
}
int get(int l, int r){
if(r == l) return rmq[l][0];
int k = __lg(r - l + 1);
return min(rmq[l][k], rmq[r - (1 << k) + 1][k]);
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n >> k;
for(int i = 1;i <= n;i++) cin >> a[i];
st.push(0); a[0] = 1e18;
for(int i = 1;i <= n;i++){
while(!st.empty() && a[st.top()] <= a[i]) st.pop();
if(!st.empty()) L[i] = 1;
else L[i] = st.top() + 1;
st.push(i);
}
memset(dp, 0x4f, sizeof(dp));
dp[0][0] = 0;
for(int j = 1;j <= k;j++){
build(j - 1);
for(int i = 1;i <= n;i++){
dp[i][j] = get(L[i] - 1, i - 1) + a[i];
dp[i][j] = min(dp[i][j], dp[L[i] - 1][j]);
}
}
cout << dp[n][k];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
86608 KB |
Output is correct |
2 |
Incorrect |
11 ms |
86600 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
86728 KB |
Output is correct |
2 |
Incorrect |
12 ms |
86572 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
86608 KB |
Output is correct |
2 |
Incorrect |
11 ms |
86600 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
86608 KB |
Output is correct |
2 |
Incorrect |
11 ms |
86600 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |