Untitled

 avatar
unknown
c_cpp
9 days ago
617 B
4
Indexable
#include <iostream>
#include<vector>
#include<algorithm>
const int inf = 2e9 + 5;
using namespace std;

int main() {
	int n, x, ans = 1; cin >> n >> x;
	vector<int> a(n);
	for (auto& v : a) cin >> v;

	vector<int> arr(n, inf), res(n);
	for (int i = 0; i < n; ++i) {
		auto pos = lower_bound(arr.begin(), arr.end(), a[i]) - arr.begin();
		arr[pos] = a[i], res[i] = pos + 1;
	}

	arr.assign(n, inf);
	for (int i = n - 1; i >= 0; --i)
		ans = max(ans, res[i] + (int)(lower_bound(arr.begin(), arr.end(), -a[i] + x) - arr.begin())),
		arr[lower_bound(arr.begin(), arr.end(), -a[i]) - arr.begin()] = -a[i];

	cout << ans;
}
Leave a Comment