This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
//#include<iostream>
//#include<vector>
using namespace std;
#define _ << ' ' <<
#define pb push_back
#define all(x) begin(x), end(x)
#define mp make_pair
#define f first
#define s second
#define sz(x) int((x).size())
using ll = long long;
using db = long double;
using pl = pair<ll,ll>;
using pi = pair<int,int>;
void trad(vector<ll>a, ll x){
int n=a.size();
set < pl > lis;
vector < ll > tr(n,1);
for(int i=n-1; i>=0; i--){
auto p = lis.upper_bound({a[i],1e15});
if(p!=lis.end()){
tr[i]+=p->s;
}
lis.insert({a[i],tr[i]});
auto pp = lis.find({a[i],tr[i]});
if(next(pp)!=lis.end() and (next(pp)->s == tr[i])) lis.erase(pp);
else if(pp!=lis.begin() and (prev(pp)->s == tr[i])) lis.erase(prev(pp));
}
ll ans = *max_element(all(tr));
//for(auto k: tr) cout << k << ' ';
//cout << endl;
set < pl > Lis;
for(int i=0; i<n-1; i++){
auto p = Lis.upper_bound({a[i]-x,0});
if(p!=Lis.end()){
if(p->f > a[i]-x){
Lis.insert({a[i]-x, p->s});
Lis.erase(p);
}
}
else Lis.insert({a[i]-x, Lis.size()+1});
auto w = Lis.lower_bound({a[i+1],0});
ll ct=Lis.size();
if(w!=Lis.end()) ct = (w->s)-1;
ans = max(ans, ct + tr[i+1]);
}
cout << ans << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
//freopen("nocross.in", "r", stdin);
//freopen("nocross.out", "w", stdout);
int n; ll x;
cin >> n >> x;
vector < ll > a(n);
for(auto &w: a) cin >> w;
trad(a,x);
}
# | 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... |