#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
struct SegTree {
int n;
vector<int> st;
SegTree(int _n) : n(_n), st(2 * _n, 0) {}
void update(int p, int v) {
p += n;
st[p] = max(st[p], v);
for (p >>= 1; p; p >>= 1) {
st[p] = max(st[p << 1], st[p << 1 | 1]);
}
}
int query(int l, int r) {
int res = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) res = max(res, st[l++]);
if (r & 1) res = max(res, st[--r]);
}
return res;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
ll m;
cin >> n >> m;
vector<ll> arr(n), vals;
vals.reserve(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
vals.push_back(arr[i]);
}
sort(vals.begin(), vals.end());
vals.erase(unique(vals.begin(), vals.end()), vals.end());
int len = vals.size();
SegTree st(len);
int best = 0;
for (int i = 0; i < n; i++) {
ll h = arr[i];
int dpi = 0;
if (h <= m) dpi = 1;
ll need = h - m;
auto it = lower_bound(vals.begin(), vals.end(), need);
if (it != vals.end()) {
int l = int(it - vals.begin());
int q = st.query(l, len);
if (q > 0) dpi = max(dpi, q + 1);
}
if (dpi > 0) {
int idx = int(lower_bound(vals.begin(), vals.end(), h) - vals.begin());
st.update(idx, dpi);
best = max(best, dpi);
}
}
cout << (n - best) << endl;
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... |