This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* IN THE NAME OF GOD */
/* |\/| /-\ |\| | |\/| /-\ */
#include "bits/stdc++.h"
using namespace std;
#define sz(x) (int)x.size()
#define endl '\n'
#define pb push_back
#define all(x) x.begin(), x.end()
#define F first
#define S second
#define mr make_pair
//#define int long long
#define pii pair<int, int>
typedef long double ld;
typedef long long ll;
mt19937 rng (chrono::high_resolution_clock::now().time_since_epoch().count());
const int N = 4e5 + 10;
const int MOD = 1e9 + 7;
const int inf = 1e9 + 1;
int a[N], fen[N], l[N], r[N];
vector<int> vec, dp;
void upd(int p, int x){
for(; p < N; p += p & -p)
fen[p] = max(fen[p], x);
}
int get(int p){
int ret = 0;
for(; p > 0; p -= p & -p)
ret = max(ret, fen[p]);
return ret;
}
int f(int x){
return lower_bound(all(vec), x) - vec.begin() + 1;
}
int32_t main(){
ios_base:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, x;
cin >> n >> x;
for(int i = 1; i <= n; i++){
cin >> a[i];
vec.pb(a[i]);
vec.pb(a[i] + x);
}
sort(all(vec));
vec.resize(unique(all(vec)) - vec.begin());
for(int i = 1; i <= n; i++){
int t = lower_bound(all(dp), a[i]) - dp.begin();
l[i] = t + 1;
if(t < sz(dp))
dp[t] = a[i];
else
dp.pb(a[i]);
}
reverse(a + 1, a + n + 1);
for(int i = 1; i <= n; i++)
a[i] *= -1;
dp.clear();
for(int i = 1; i <= n; i++){
int t = lower_bound(all(dp), a[i]) - dp.begin();
r[n - i + 1] = t + 1;
if(t < sz(dp))
dp[t] = a[i];
else
dp.pb(a[i]);
}
reverse(a + 1, a + n + 1);
for(int i = 1; i <= n; i++)
a[i] *= -1;
int ans = 0;
for(int i = 1; i <= n; i++){
ans = max(ans, r[i] + get(f(a[i] + x) - 1));
upd(f(a[i]), l[i]);
}
cout << ans;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |