This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimization "Ofast"
#pragma GCC optimization "unroll-loop"
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
using namespace std;
typedef pair<ll,ll> LL;
const ll N = 2e5 + 9;
const ll lim = 1e6 + 9;
const ll inf = 1e16 + 7;
ll n,x,a[N],lis[N],lds[N],ans;
vector<ll> v;
ll Find(ll x){
return lower_bound(v.begin(),v.end(),x) - v.begin() + 1;
}
void compress(){
sort(v.begin(),v.end()); v.resize(unique(v.begin(),v.end()) - v.begin());
}
ll bit[lim],st[4*lim];
void upd(ll p,ll val){
for (ll i = p;i < lim;i += i & -i) bit[i] = max(bit[i],val);
}
ll Get(ll p){
ll res = 0;
for (ll i = p;i > 0;i -= i & -i) res = max(bit[i],res);
return res;
}
void update(ll id,ll l,ll r,ll u,ll val){
if (u < l||r < u) return;
if (l == r){
st[id] = val; return;
}
ll mid = (l + r)/2;
update(id*2,l,mid,u,val); update(id*2 + 1,mid + 1,r,u,val);
st[id] = max(st[id*2],st[id*2 + 1]);
}
ll Get_ans(ll id,ll l,ll r,ll u,ll v){
if (v < l||r < u) return 0;
if (u <= l&&r <= v) return st[id];
ll mid = (l + r)/2;
return max(Get_ans(id*2,l,mid,u,v),Get_ans(id*2 + 1,mid + 1,r,u,v));
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define task "test"
if (fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin>>n>>x;
for (ll i = 1;i <= n;i++) cin>>a[i],v.push_back(a[i]),v.push_back(a[i] + x);
compress();
for (ll i = n;i > 0;i--){
lds[i] = Get_ans(1,1,lim - 1,Find(a[i]) + 1,lim - 1) + 1;
update(1,1,lim - 1,Find(a[i]),lds[i]);
}
for (ll i = 1;i <= n;i++){
lis[i] = Get(Find(a[i]) - 1) + 1; //cout<<Get(Find(a[i] + x) - 1) <<" "<< lds[i]<<"\n";
//cout<<i<<": "<<Get(Find(a[i] + x) - 1)<<" "<<lds[i]<<"\n";
ans = max(ans,Get(Find(a[i] + x) - 1) + lds[i]); upd(Find(a[i]),lis[i]);
}
//return 0;
//for (ll i = 1;i <= n;i++) cout<<lds[i]<<" "; return 0;
cout<<ans;
}
Compilation message (stderr)
glo.cpp:1: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
1 | #pragma GCC optimization "Ofast"
|
glo.cpp:2: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
2 | #pragma GCC optimization "unroll-loop"
|
glo.cpp: In function 'int main()':
glo.cpp:59:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
59 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
glo.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
60 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |