#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#include <algorithm>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <map>
#include <set>
using namespace std;
vector<int64_t> p;
const int MX = 131072;
//const int MX = 30;
struct SparseTable {
int64_t dp[MX][18];
int64_t query (int l, int r) {
int sz = log2(r - l + 1);
return min(dp[l][sz], dp[r - (1 << sz) + 1][sz]);
}
void upd (vector<int64_t> &a) {
for (int j = 0; j < 18; j++) {
for (int i = 0; i < a.size(); i++) {
if (j == 0) {
dp[i][j] = a[i];
} else {
dp[i][j] = min(dp[i][j - 1], dp[min(i + (1 << (j - 1)), (int)a.size() - 1)][j - 1]);
}
}
}
}
};
int64_t query (vector<int64_t>& val, int l, int r) {
int64_t ans = 1e17;
for (int i = l; i <= r; i++) {
ans = min(ans, val[i]);
}
return ans;
}
void process (vector<int64_t> &val, vector<pair<pair<int,int>, int64_t>> &intervals) {
for (auto& p: intervals) {
for (int x = p.first.first; x <= p.first.second; x++) {
val[x] = p.second;
}
}
return;
reverse(intervals.begin(), intervals.end());
set<int> unused;
for (int i = 0; i < val.size(); i++) {
unused.insert(i);
}
for (auto& p: intervals) {
while (unused.lower_bound(p.first.first) != unused.end() and *unused.lower_bound(p.first.first) <= p.first.second) {
auto it = unused.lower_bound(p.first.first);
val[*it] = p.second;
unused.erase(*it);
}
}
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, K;
cin >> N >> K;
vector<int64_t> arr(N);
for (int i = 0; i < N; i++) {
cin >> arr[i];
}
int64_t INF = 1e17;
vector<int64_t> cur(N + 1);
p.assign(N + 1, INF), cur.assign(N + 1, INF);
for (int j = 1; j <= N; j++) {
p[j] = ((j == 1) ? arr[0] : max(p[j - 1], arr[j - 1]));
}
for (int i = 0; i < arr.size(); i++) {
arr[i] = -arr[i];
}
SparseTable st_a;
st_a.upd(arr);
for (int i = 0; i < arr.size(); i++) {
arr[i] = -arr[i];
}
int nxt[N];
for (int j = 0; j < N; j++) {
int l = 0;
int r = j;
while (l != r) {
int m = (l + r)/2;
if (st_a.query(m, j) == -arr[j]) {
r = m;
} else {
l = m + 1;
}
}
nxt[j] = l;
}
while (__builtin_popcount(p.size()) != 1) {
p.push_back(0);
cur.push_back(0);
}
SparseTable st_p;
vector<int64_t> val;
val.assign(N + 1, 0);
for (int i = 2; i <= K; i++) {
for (int j = 0; j < val.size(); j++) {
val[j] = 0;
}
st_p.upd(p);
vector<pair<pair<int,int>,int64_t>> vec;
for (int j = 1; j <= N; j++) {
vec.push_back(make_pair(make_pair(nxt[j - 1], j - 1), arr[j - 1]));
}
process(val, vec);
for (int j = 0; j < val.size(); j++) {
val[j] += p[j];
}
for (int j = 1; j <= N; j++) {
if (j == 1) {
cur[j] = p[0] + arr[0];
} else {
cur[j] = min(query(val, 0, nxt[j - 1] - 1), arr[j - 1] + st_p.query(nxt[j - 1], j - 1));
}
}
swap(cur, p);
if (p == cur) {
break;
}
}
cout << p[N];
}
Compilation message
blocks.cpp:7: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
7 | #pragma GCC optimization ("O3")
|
blocks.cpp:8: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
8 | #pragma GCC optimization ("unroll-loops")
|
blocks.cpp: In member function 'void SparseTable::upd(std::vector<long int>&)':
blocks.cpp:23:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for (int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
blocks.cpp: In function 'void process(std::vector<long int>&, std::vector<std::pair<std::pair<int, int>, long int> >&)':
blocks.cpp:49:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (int i = 0; i < val.size(); i++) {
| ~~^~~~~~~~~~~~
blocks.cpp: In function 'int main()':
blocks.cpp:75:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for (int i = 0; i < arr.size(); i++) {
| ~~^~~~~~~~~~~~
blocks.cpp:80:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for (int i = 0; i < arr.size(); i++) {
| ~~^~~~~~~~~~~~
blocks.cpp:105:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for (int j = 0; j < val.size(); j++) {
| ~~^~~~~~~~~~~~
blocks.cpp:114:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | for (int j = 0; j < val.size(); j++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
37204 KB |
Output is correct |
2 |
Correct |
17 ms |
37140 KB |
Output is correct |
3 |
Correct |
16 ms |
37132 KB |
Output is correct |
4 |
Correct |
15 ms |
37204 KB |
Output is correct |
5 |
Correct |
17 ms |
37148 KB |
Output is correct |
6 |
Correct |
15 ms |
37196 KB |
Output is correct |
7 |
Correct |
15 ms |
37248 KB |
Output is correct |
8 |
Correct |
15 ms |
37248 KB |
Output is correct |
9 |
Correct |
15 ms |
37112 KB |
Output is correct |
10 |
Correct |
14 ms |
37204 KB |
Output is correct |
11 |
Correct |
15 ms |
37244 KB |
Output is correct |
12 |
Correct |
15 ms |
37204 KB |
Output is correct |
13 |
Correct |
15 ms |
37216 KB |
Output is correct |
14 |
Correct |
14 ms |
37236 KB |
Output is correct |
15 |
Correct |
15 ms |
37204 KB |
Output is correct |
16 |
Correct |
15 ms |
37180 KB |
Output is correct |
17 |
Correct |
16 ms |
37188 KB |
Output is correct |
18 |
Correct |
15 ms |
37204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
37204 KB |
Output is correct |
2 |
Correct |
15 ms |
37188 KB |
Output is correct |
3 |
Correct |
15 ms |
37144 KB |
Output is correct |
4 |
Correct |
15 ms |
37204 KB |
Output is correct |
5 |
Correct |
15 ms |
37148 KB |
Output is correct |
6 |
Correct |
15 ms |
37204 KB |
Output is correct |
7 |
Correct |
15 ms |
37176 KB |
Output is correct |
8 |
Correct |
16 ms |
37176 KB |
Output is correct |
9 |
Correct |
16 ms |
37136 KB |
Output is correct |
10 |
Correct |
19 ms |
37204 KB |
Output is correct |
11 |
Correct |
19 ms |
37124 KB |
Output is correct |
12 |
Correct |
15 ms |
37176 KB |
Output is correct |
13 |
Correct |
16 ms |
37160 KB |
Output is correct |
14 |
Correct |
16 ms |
37168 KB |
Output is correct |
15 |
Correct |
16 ms |
37156 KB |
Output is correct |
16 |
Correct |
15 ms |
37204 KB |
Output is correct |
17 |
Correct |
15 ms |
37136 KB |
Output is correct |
18 |
Correct |
16 ms |
37196 KB |
Output is correct |
19 |
Correct |
16 ms |
37244 KB |
Output is correct |
20 |
Correct |
15 ms |
37252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
37204 KB |
Output is correct |
2 |
Correct |
17 ms |
37140 KB |
Output is correct |
3 |
Correct |
16 ms |
37132 KB |
Output is correct |
4 |
Correct |
15 ms |
37204 KB |
Output is correct |
5 |
Correct |
17 ms |
37148 KB |
Output is correct |
6 |
Correct |
15 ms |
37196 KB |
Output is correct |
7 |
Correct |
15 ms |
37248 KB |
Output is correct |
8 |
Correct |
15 ms |
37248 KB |
Output is correct |
9 |
Correct |
15 ms |
37112 KB |
Output is correct |
10 |
Correct |
14 ms |
37204 KB |
Output is correct |
11 |
Correct |
15 ms |
37244 KB |
Output is correct |
12 |
Correct |
15 ms |
37204 KB |
Output is correct |
13 |
Correct |
15 ms |
37216 KB |
Output is correct |
14 |
Correct |
14 ms |
37236 KB |
Output is correct |
15 |
Correct |
15 ms |
37204 KB |
Output is correct |
16 |
Correct |
15 ms |
37180 KB |
Output is correct |
17 |
Correct |
16 ms |
37188 KB |
Output is correct |
18 |
Correct |
15 ms |
37204 KB |
Output is correct |
19 |
Correct |
14 ms |
37204 KB |
Output is correct |
20 |
Correct |
15 ms |
37188 KB |
Output is correct |
21 |
Correct |
15 ms |
37144 KB |
Output is correct |
22 |
Correct |
15 ms |
37204 KB |
Output is correct |
23 |
Correct |
15 ms |
37148 KB |
Output is correct |
24 |
Correct |
15 ms |
37204 KB |
Output is correct |
25 |
Correct |
15 ms |
37176 KB |
Output is correct |
26 |
Correct |
16 ms |
37176 KB |
Output is correct |
27 |
Correct |
16 ms |
37136 KB |
Output is correct |
28 |
Correct |
19 ms |
37204 KB |
Output is correct |
29 |
Correct |
19 ms |
37124 KB |
Output is correct |
30 |
Correct |
15 ms |
37176 KB |
Output is correct |
31 |
Correct |
16 ms |
37160 KB |
Output is correct |
32 |
Correct |
16 ms |
37168 KB |
Output is correct |
33 |
Correct |
16 ms |
37156 KB |
Output is correct |
34 |
Correct |
15 ms |
37204 KB |
Output is correct |
35 |
Correct |
15 ms |
37136 KB |
Output is correct |
36 |
Correct |
16 ms |
37196 KB |
Output is correct |
37 |
Correct |
16 ms |
37244 KB |
Output is correct |
38 |
Correct |
15 ms |
37252 KB |
Output is correct |
39 |
Incorrect |
16 ms |
37260 KB |
Output isn't correct |
40 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
37204 KB |
Output is correct |
2 |
Correct |
17 ms |
37140 KB |
Output is correct |
3 |
Correct |
16 ms |
37132 KB |
Output is correct |
4 |
Correct |
15 ms |
37204 KB |
Output is correct |
5 |
Correct |
17 ms |
37148 KB |
Output is correct |
6 |
Correct |
15 ms |
37196 KB |
Output is correct |
7 |
Correct |
15 ms |
37248 KB |
Output is correct |
8 |
Correct |
15 ms |
37248 KB |
Output is correct |
9 |
Correct |
15 ms |
37112 KB |
Output is correct |
10 |
Correct |
14 ms |
37204 KB |
Output is correct |
11 |
Correct |
15 ms |
37244 KB |
Output is correct |
12 |
Correct |
15 ms |
37204 KB |
Output is correct |
13 |
Correct |
15 ms |
37216 KB |
Output is correct |
14 |
Correct |
14 ms |
37236 KB |
Output is correct |
15 |
Correct |
15 ms |
37204 KB |
Output is correct |
16 |
Correct |
15 ms |
37180 KB |
Output is correct |
17 |
Correct |
16 ms |
37188 KB |
Output is correct |
18 |
Correct |
15 ms |
37204 KB |
Output is correct |
19 |
Correct |
14 ms |
37204 KB |
Output is correct |
20 |
Correct |
15 ms |
37188 KB |
Output is correct |
21 |
Correct |
15 ms |
37144 KB |
Output is correct |
22 |
Correct |
15 ms |
37204 KB |
Output is correct |
23 |
Correct |
15 ms |
37148 KB |
Output is correct |
24 |
Correct |
15 ms |
37204 KB |
Output is correct |
25 |
Correct |
15 ms |
37176 KB |
Output is correct |
26 |
Correct |
16 ms |
37176 KB |
Output is correct |
27 |
Correct |
16 ms |
37136 KB |
Output is correct |
28 |
Correct |
19 ms |
37204 KB |
Output is correct |
29 |
Correct |
19 ms |
37124 KB |
Output is correct |
30 |
Correct |
15 ms |
37176 KB |
Output is correct |
31 |
Correct |
16 ms |
37160 KB |
Output is correct |
32 |
Correct |
16 ms |
37168 KB |
Output is correct |
33 |
Correct |
16 ms |
37156 KB |
Output is correct |
34 |
Correct |
15 ms |
37204 KB |
Output is correct |
35 |
Correct |
15 ms |
37136 KB |
Output is correct |
36 |
Correct |
16 ms |
37196 KB |
Output is correct |
37 |
Correct |
16 ms |
37244 KB |
Output is correct |
38 |
Correct |
15 ms |
37252 KB |
Output is correct |
39 |
Incorrect |
16 ms |
37260 KB |
Output isn't correct |
40 |
Halted |
0 ms |
0 KB |
- |