#include <iostream>
#include <stdio.h>
#include <deque>
using namespace std;
typedef long long ll;
const int N = 1e5 + 11;
const ll oo = 1e18;
int n,k,lastGreater[N];
ll f[N][101],a[N],minf[N];
deque <int> Q;
void init(){
ll cur = -1;
for(int i = 1; i <= n; ++i){
cur = max(cur,a[i]);
f[i][1] = cur;
}
for(int i = 1; i <= n; ++i){
lastGreater[i] = 0;
while(Q.size() && a[Q.back()] <= a[i]) Q.pop_back();
if(Q.size()) lastGreater[i] = Q.back();
Q.push_back(i);
}
//for(int i = 1; i <= n; ++i) cout << i << " ~> " << lastGreater[i] << endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie();
cout.tie();
cin >> n >> k;
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 0; i <= n; ++i)
for(int j = 1; j <= k; ++j) f[i][j] = oo;
init();
minf[0] = oo;
for(int j = 2; j <= k; ++j){
Q.clear();
for(int i = 1; i <= n; ++i){
int l = lastGreater[i];
while(!Q.empty() && Q.front() < l) Q.pop_front();
while(!Q.empty() && f[Q.back()][j - 1] >= f[i - 1][j - 1]) Q.pop_back();
Q.push_back(i - 1);
minf[i] = min(oo,f[Q.front()][j - 1] + a[i]);
}
for(int i = 1; i <= n; ++i){
f[i][j] = minf[i] = min(minf[i],minf[lastGreater[i]]);
}
/*for(int i = 1; i <= n; ++i){
ll cc = 0;
for(int v = i; v > 0; --v){
cc = max(cc,a[v]);
f[i][j] = min(f[i][j],f[v - 1][j - 1] + cc);
}
}*/
}
cout << f[n][k];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
380 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
8440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |