#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define FOD(i, a, b) for(int i = (a); i >= (b); i--)
#define file(name) if(fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define el cout << '\n'
#define maxn int(2e5 + 5)
using namespace std;
int n, m, k, a[maxn], dp[maxn], t[maxn << 1][2];
vector<int> vals;
void update(int x, int v, bool o) {
if(o) for(; x <= m; x += x & -x) t[x][o] = max(t[x][o], v);
else for(; x; x -= x & -x) t[x][o] = max(t[x][o], v);
}
int get(int x, bool o) {
int res = 0;
if(!o) for(; x; x -= x & -x) res = max(res, t[x][o]);
else for(; x <= m; x += x & -x) res = max(res, t[x][o]);
return res;
}
void compress() {
FOR(i, 1, n) vals.push_back(a[i]), vals.push_back(a[i] - k);
sort(vals.begin(), vals.end());
vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
m = vals.size();
}
/**
0: increase
1: decrease
**/
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
compress();
FOR(i, 1, n) {
int x = lower_bound(vals.begin(), vals.end(), a[i]) - vals.begin() + 1;
int y = upper_bound(vals.begin(), vals.begin(), a[i] - k) - vals.begin() + 1;
if(max(get(x, 1), get(y, 0)) == 0) dp[i] = (a[i] <= k);
else dp[i] = max(get(x, 1), get(y, 0)) + 1;
update(x, dp[i], 0);
update(x, dp[i], 1);
}
cout << n - dp[n];
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |