#include <bits/stdc++.h>
#pragma GCC optimize("trapv")
using namespace std;
const int N = 200'005;
struct bit {
int tree[N], n;
void init(int _n) {
n = _n;
for (int i = 0; i < n; i++) {
tree[i] = 2e9;
}
}
void upd(int x, int v) {
for (int i = x; i < n; i = (i | (i + 1))) {
tree[i] = min(tree[i], v);
}
}
int query(int r) {
int ans = 2e9;
if (r < 0) {
return ans;
}
for (int i = r; i >= 0; i = (i & (i + 1)) - 1) {
ans = min(ans, tree[i]);
}
return ans;
}
} bit1, bit2;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
int a[n + 1];
a[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<int> vals1, vals2, v1, v2;
for (int i = 0; i < n - 1; i++) {
vals1.push_back(a[i + 1]);
vals2.push_back(a[i + 1] - i * m);
}
sort(vals1.begin(), vals1.end());
sort(vals2.begin(), vals2.end());
for (int i = 0; i < vals1.size(); i++) {
if (i == 0 || vals1[i] != vals1[i - 1]) {
v1.push_back(vals1[i]);
}
}
for (int i = 0; i < vals2.size(); i++) {
if (i == 0 || vals2[i] != vals2[i - 1]) {
v2.push_back(vals2[i]);
}
}
n++;
int dp[n + 1];
dp[n - 1] = dp[n] = 0;
bit1.init(v1.size());
bit2.init(v2.size());
for (int i = n - 2; i >= 0; i--) {
dp[i] = dp[n] + n - 1;
int idx = upper_bound(v1.begin(), v1.end(), a[i] + 2 * m) - v1.begin() - 1;
dp[i] = min(dp[i], bit1.query(idx));
idx = lower_bound(v1.begin(), v1.end(), a[i + 1]) - v1.begin();
bit1.upd(idx, dp[i + 1] + i);
idx = lower_bound(v2.begin(), v2.end(), a[i + 1] - i * m) - v2.begin();
bit2.upd(idx, dp[i + 1] + i);
idx = upper_bound(v2.begin(), v2.end(), a[i] - i * m + m) - v2.begin() - 1;
dp[i] = min(dp[i], bit2.query(idx));
// for (int j = i + 1; j < n - 1; j++) {
// if (a[j + 1] <= a[i] + 2 * m) {
// dp[i] = min(dp[i], dp[j + 1] + j);
// }
// }
// for (int j = i; j < n - 1; j++) {
// if (a[j + 1] - j * m <= a[i] - i * m + m) {
// dp[i] = min(dp[i], dp[j + 1] + j);
// }
// }
dp[i] -= i;
if (a[i] >= a[i + 1] || a[i + 1] - a[i] <= m) {
dp[i] = min(dp[i], dp[i + 1]);
}
}
cout << dp[0] << '\n';
return 0;
}
Compilation message
triusis.cpp: In function 'int main()':
triusis.cpp:46:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for (int i = 0; i < vals1.size(); i++) {
| ~~^~~~~~~~~~~~~~
triusis.cpp:51:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int i = 0; i < vals2.size(); i++) {
| ~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
3 ms |
604 KB |
Output is correct |
23 |
Correct |
3 ms |
604 KB |
Output is correct |
24 |
Correct |
3 ms |
600 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
3 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
600 KB |
Output is correct |
32 |
Correct |
2 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
600 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
344 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
4 ms |
600 KB |
Output is correct |
35 |
Correct |
4 ms |
604 KB |
Output is correct |
36 |
Correct |
4 ms |
692 KB |
Output is correct |
37 |
Correct |
3 ms |
604 KB |
Output is correct |
38 |
Correct |
2 ms |
604 KB |
Output is correct |
39 |
Correct |
2 ms |
604 KB |
Output is correct |
40 |
Correct |
2 ms |
600 KB |
Output is correct |
41 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
42 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
600 KB |
Output is correct |
2 |
Correct |
4 ms |
604 KB |
Output is correct |
3 |
Correct |
4 ms |
692 KB |
Output is correct |
4 |
Correct |
3 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
600 KB |
Output is correct |
8 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |