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 <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// #pragma GCC target ("avx,avx2,fma")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define SPEED ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0)
#define rall(v) (v).rbegin(),(v).rend()
#define all(v) (v).begin(),(v).end()
#define setp fixed<<setprecision
#define OK cerr<<"OK"<<endl<<flush
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define F first
#define S second
#define y0 jahdakdh
#define y1 jahsadakdakdh
#define endl '\n'
const ll MOD=1e9+7;
const ll mod=(1ll<<31)-1;
const ld eps=1e-8;
const ll MAXLONG=9223372036854775807;
const ll MINLONG=-9223372036854775807;
using namespace std;
mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count());
const int N=2e5+5;
ll n, x, l[N], r[N], a[N], ans, fs[N];
void LIS()
{
vector<int>v{a[0]};
l[0]=1;
r[n-1]=1;
for(int i=1; i<n; i++)
{
if(v.back()<a[i])
{
v.pb(a[i]);
r[n-i-1]=v.size();
if(a[i]>0) l[i]=v.size();
}
else
{
int pos=lower_bound(all(v), a[i])-v.begin();
v[pos]=a[i];
r[n-i-1]=pos+1;
if(a[i]>0) l[i]=pos+1;
}
}
}
int main()
{
SPEED;
cin>>n>>x;
for(int i=0; i<n; i++)
cin>>a[i];
LIS();
reverse(a, a+n);
for(int i=0; i<n; i++)
a[i]=-a[i];
LIS();
reverse(a, a+n);
for(int i=0; i<n; i++)
a[i]=-a[i];
a[n]=2*MOD;
for(int i=n-1; i>=0; i--)
{
fs[i]=i+1;
while(a[fs[i]]+x<=a[i]) fs[i]=fs[fs[i]];
//cout<<i<<' '<<l[i]<<' '<<r[i]<<' '<<fs[i]<<endl;
ans=max(ans, l[i]+r[fs[i]]);
}
cout<<ans<<endl;
return 0;
}
Compilation message (stderr)
glo.cpp: In function 'void LIS()':
glo.cpp:36:21: warning: narrowing conversion of 'a[0]' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
36 | vector<int>v{a[0]};
| ~~~^
glo.cpp:36:21: warning: narrowing conversion of 'a[0]' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
# | 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... |