#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <queue>
#include <string>
#include <string.h>
#include <math.h>
#include <iomanip>
using namespace std;
#define int64 long long
#define pii pair <int64, int64>
#define fr first
#define sc second
#define heap priority_queue
struct piii{
int64 fr, sc, th;
bool operator < (const piii &b){
if (fr != b.fr) return fr < b.fr;
if (sc != b.sc) return sc < b.sc;
return th < b.th;
}
};
///////////////////////////////////////////////
///////////////////////////////////////////////
int n, k;
int64 a[100005];
int64 f[105][100005];
stack <int> st;
stack <int64> mx;
int main(){
cout << setprecision(2) << fixed;
ios::sync_with_stdio(false), cin.tie(0);
//freopen("input.txt", "r", stdin);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
a[0] = 1e9;
for (int i = 1; i <= n; i++)
for (int j = 0; j <= k; j++)
f[j][i] = 1e12;
f[0][0] = 0;
for (int j = 1; j <= k; j++){
while (!st.empty()) st.pop(), mx.pop();
a[j - 1] = 1e12;
st.push(j - 1);
mx.push(1e12);
for (int i = j; i <= n; i++){
while (st.size() && a[i] >= a[st.top()]){
int tmp = st.top();
st.pop();
mx.pop();
f[j - 1][st.top()] = min(f[j - 1][st.top()], f[j - 1][tmp]);
}
mx.push(min(mx.top(), f[j - 1][st.top()] + a[i]));
st.push(i);
f[j][i] = mx.top();
}
/*
int64 tmp = f[j][n];
for (int i = n - 1; i >= 1; i--){
tmp = min(tmp, f[j][i]);
//f[j][i] = tmp;
}
*/
//for (int i = 1; i <= n; i++) cout << f[j][i] << ' '; cout << endl;
}
cout << f[k][n];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
380 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
396 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
380 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
3 ms |
1016 KB |
Output is correct |
16 |
Correct |
3 ms |
1016 KB |
Output is correct |
17 |
Correct |
3 ms |
636 KB |
Output is correct |
18 |
Correct |
3 ms |
988 KB |
Output is correct |
19 |
Correct |
3 ms |
1016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
2680 KB |
Output is correct |
2 |
Correct |
9 ms |
1400 KB |
Output is correct |
3 |
Correct |
17 ms |
3548 KB |
Output is correct |
4 |
Correct |
117 ms |
27768 KB |
Output is correct |
5 |
Correct |
337 ms |
80888 KB |
Output is correct |
6 |
Correct |
24 ms |
4984 KB |
Output is correct |
7 |
Correct |
159 ms |
37908 KB |
Output is correct |
8 |
Correct |
2 ms |
476 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
632 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
42 ms |
8952 KB |
Output is correct |
15 |
Correct |
4 ms |
760 KB |
Output is correct |
16 |
Correct |
14 ms |
2680 KB |
Output is correct |
17 |
Correct |
10 ms |
1656 KB |
Output is correct |
18 |
Correct |
19 ms |
3812 KB |
Output is correct |
19 |
Correct |
138 ms |
28276 KB |
Output is correct |
20 |
Correct |
398 ms |
81016 KB |
Output is correct |
21 |
Correct |
25 ms |
4984 KB |
Output is correct |
22 |
Correct |
188 ms |
37880 KB |
Output is correct |
23 |
Correct |
18 ms |
3448 KB |
Output is correct |
24 |
Correct |
50 ms |
10360 KB |
Output is correct |
25 |
Correct |
397 ms |
80968 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
504 KB |
Output is correct |
28 |
Correct |
37 ms |
8952 KB |
Output is correct |
29 |
Correct |
4 ms |
760 KB |
Output is correct |
30 |
Correct |
11 ms |
2680 KB |
Output is correct |
31 |
Correct |
9 ms |
1912 KB |
Output is correct |
32 |
Correct |
18 ms |
4060 KB |
Output is correct |
33 |
Correct |
120 ms |
28408 KB |
Output is correct |
34 |
Correct |
350 ms |
81408 KB |
Output is correct |
35 |
Correct |
24 ms |
5368 KB |
Output is correct |
36 |
Correct |
165 ms |
38264 KB |
Output is correct |
37 |
Correct |
4 ms |
760 KB |
Output is correct |
38 |
Correct |
36 ms |
8952 KB |
Output is correct |
39 |
Correct |
3 ms |
504 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |