#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;
assert(n < N);
for (int i = 0; i < n; i++) {
tree[i] = 2e9;
}
}
void upd(int x, int v) {
assert(x < n);
for (int i = x; i < n; i = (i | (i + 1))) {
tree[i] = min(tree[i], v);
}
}
int query(int r) {
assert(r < n);
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:49:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (int i = 0; i < vals1.size(); i++) {
| ~~^~~~~~~~~~~~~~
triusis.cpp:54:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int i = 0; i < vals2.size(); i++) {
| ~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |