#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,m = 0,lastGreater[N],fqueue[N],fw = 0,pos[N];
ll f[N][100],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();
for(int j = 2; j <= k; ++j){
Q.clear();
m = 0;
minf[0] = oo;
for(int i = 1; i <= n; ++i){
minf[i] = oo;
pos[i] = 0;
while(Q.size() && Q.front() <= lastGreater[i]) Q.pop_front();
while(Q.size() && f[Q.back()][j - 1] >= f[i - 1][j - 1]){
Q.pop_back();
}
Q.push_back(i - 1);
minf[i] = f[Q.front()][j - 1] + a[i];
}
for(int i = 1; i <= n; ++i){
f[i][k] = minf[i] = min(minf[i],minf[lastGreater[i]]);
}
}
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 |
4 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 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
248 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 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
8440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |