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>
#define N 200005
using namespace std;
int t[N];
struct SegTree{
vector<int> t;
int n;
SegTree(int sz){
n = sz + 5;
t.assign(4*n,0);
}
void clear(){
t.assign(4*n,0);
}
void upd(int v,int tl,int tr,int pos,int val){
if(tl == tr){
t[v] = max(t[v],val);
return;
}
int tm = (tl + tr)/2;
if(pos <= tm)
upd(v*2,tl,tm,pos,val);
else upd(v*2+1,tm+1,tr,pos,val);
t[v] = max(t[v*2],t[v*2+1]);
}
int get(int v,int tl,int tr,int l,int r){
if(tr < l || r < tl)
return 0;
if(l <= tl && tr <= r){
return t[v];
}
int tm = (tl + tr)/2;
return max(get(v*2,tl,tm,l,r),get(v*2+1,tm+1,tr,l,r));
}
void upd(int pos,int val){
upd(1,1,n,pos,val);
}
int get(int l,int r){
return get(1,1,n,l,r);
}
};
pair<int,int> sufquery[N];
pair<int,int> prequery[N];
void solve(){
int n,x;
cin >> n >> x;
int best = 0;
vector<int> compress;
for(int i = 1;i<=n;i++){
cin >> t[i];
compress.push_back(t[i]);
compress.push_back(t[i]+x);
compress.push_back(t[i]-x);
}
sort(compress.begin(),compress.end());
compress.resize(unique(compress.begin(),compress.end())-compress.begin());
int sz = compress.size();
SegTree tpre(sz);
SegTree tsuf(sz);
for(int i = 1;i<=n;i++){
int num = upper_bound(compress.begin(),compress.end(),t[i] -x) - compress.begin();
tpre.upd(num,tpre.get(1,num-1)+1);
best = max(best,tpre.get(1,sz));
sufquery[i+1] = {tpre.get(num,num),num};
}
//cout << best << endl;
for(int i = n;i>=1;i--){
int num = upper_bound(compress.begin(),compress.end(),t[i]+x) - compress.begin();
tsuf.upd(num,tsuf.get(num+1,sz)+1);
best = max(best,tsuf.get(1,sz));
prequery[i-1] = {tsuf.get(num,num),num};
}
//cout << best << endl;
tpre.clear();
tsuf.clear();
for(int i = 1;i<=n;i++){
int num = upper_bound(compress.begin(),compress.end(),t[i]) - compress.begin();
tpre.upd(num,tpre.get(1,num-1)+1);
best = max(best,prequery[i].first + tpre.get(1,prequery[i].second-1));
}
for(int i = n;i>=1;i--){
int num = upper_bound(compress.begin(),compress.end(),t[i]) - compress.begin();
tsuf.upd(num,tsuf.get(num+1,sz)+1);
best = max(best,sufquery[i].first + tsuf.get(sufquery[i].second+1,sz));
}
cout << best;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while(t--){
solve();
}
#ifdef Local
cout << endl << fixed << setprecision(2) << 1000.0*clock()/CLOCKS_PER_SEC << " milliseconds";
#endif
}
# | 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... |