//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <deque>
#include <chrono>
#include <cassert>
#include <bitset>
#include <random>
using namespace std;
typedef long long li;
typedef long double ld;
const li MAX = 3e5 + 20;
li inf = (li)1e15;
li mod = (li)998244353;
li dp1[MAX], dp2[MAX], p[MAX], cool[MAX];
li n, k, a[MAX];
void solve() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<li> cur;
for (int i = 1; i <= n; i++) {
while (cur.size() && a[cur.back()] < a[i]) cur.pop_back();
if (cur.size()) p[i] = cur.back();
cur.push_back(i);
}
li cur_max = a[1];
dp1[0] = inf;
dp2[0] = inf;
for (int i = 1; i <= n; i++) {
cur_max = max(cur_max, a[i]);
dp1[i] = cur_max;
}
for (int j = 2; j <= k; j++) {
vector<li> cur = { 0 };
for (int i = 1; i <= n; i++) {
dp2[i] = inf;
li uk1 = -1, uk2 = cur.size();
while (uk2 - uk1 > 1) {
li mid = (uk2 + uk1) / 2;
if (cur[mid] >= p[i]) uk2 = mid;
else uk1 = mid;
}
if (uk2 != cur.size()) dp2[i] = min(dp2[i], dp1[cur[uk2]] + a[i]);
while (cur.size() && dp1[cur.back()] > dp1[i]) cur.pop_back();
cur.push_back(i);
}
for (int i = 1; i <= n; i++) {
dp2[i] = min(dp2[i], dp2[p[i]]);
}
for (int i = 0; i <= n; i++) dp1[i] = dp2[i];
}
cout << dp1[n];
}
int main() {
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ios::sync_with_stdio(0);
li q;
q = 1;
while (q--) solve();
return 0;
}
Compilation message
blocks.cpp: In function 'void solve()':
blocks.cpp:61:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (uk2 != cur.size()) dp2[i] = min(dp2[i], dp1[cur[uk2]] + a[i]);
~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
972 KB |
Output is correct |
2 |
Correct |
7 ms |
1664 KB |
Output is correct |
3 |
Correct |
30 ms |
3008 KB |
Output is correct |
4 |
Correct |
288 ms |
3112 KB |
Output is correct |
5 |
Correct |
921 ms |
6080 KB |
Output is correct |
6 |
Correct |
33 ms |
5984 KB |
Output is correct |
7 |
Correct |
406 ms |
6060 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
29 ms |
1024 KB |
Output is correct |
15 |
Correct |
3 ms |
1024 KB |
Output is correct |
16 |
Correct |
9 ms |
1024 KB |
Output is correct |
17 |
Correct |
7 ms |
1664 KB |
Output is correct |
18 |
Correct |
17 ms |
2688 KB |
Output is correct |
19 |
Correct |
107 ms |
2688 KB |
Output is correct |
20 |
Correct |
306 ms |
5116 KB |
Output is correct |
21 |
Correct |
27 ms |
5116 KB |
Output is correct |
22 |
Correct |
139 ms |
5116 KB |
Output is correct |
23 |
Correct |
14 ms |
3456 KB |
Output is correct |
24 |
Correct |
50 ms |
5116 KB |
Output is correct |
25 |
Correct |
293 ms |
5144 KB |
Output is correct |
26 |
Correct |
0 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
13 ms |
1024 KB |
Output is correct |
29 |
Correct |
3 ms |
1024 KB |
Output is correct |
30 |
Correct |
6 ms |
1024 KB |
Output is correct |
31 |
Correct |
7 ms |
2048 KB |
Output is correct |
32 |
Correct |
14 ms |
2812 KB |
Output is correct |
33 |
Correct |
47 ms |
2844 KB |
Output is correct |
34 |
Correct |
132 ms |
5500 KB |
Output is correct |
35 |
Correct |
25 ms |
5500 KB |
Output is correct |
36 |
Correct |
74 ms |
5496 KB |
Output is correct |
37 |
Correct |
3 ms |
1024 KB |
Output is correct |
38 |
Correct |
71 ms |
1044 KB |
Output is correct |
39 |
Correct |
1 ms |
384 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
0 ms |
384 KB |
Output is correct |
42 |
Correct |
0 ms |
384 KB |
Output is correct |