#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i = (begin); i < (end); i++)
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr)
#define f first
#define s second
#define pb push_back
#define sz(x) ((int)((x).size()))
#define le(vec) vec[vec.size()-1]
#define all(x) (x).begin(), (x).end()
#define TSTS int ttt; cin >> ttt; while(ttt--) solve()
using namespace std;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long double, long double> pdd;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;
const int INF=1e9, MOD=1e9+7, mod=998244353;
const ll LINF=1e18;
void setIO()
{
FAST_IO;
}
void setIO(string s)
{
FAST_IO;
freopen((s+".in").c_str(), "r", stdin);
freopen((s+".out").c_str(), "w", stdout);
}
#define int long long
const int N=2e5+10;
int n, x, a[N], d[N], nd[N], t[4*N], idx;
set<int> st;
mii c;
void upd(int p, int x, int l=0, int r=idx+1, int id=1)
{
assert(l<=r);
if(l==r){
t[id]=max(t[id], x);
return;
}
int m=(l+r)/2;
if(p<=m) upd(p, x, l, m, id*2);
else upd(p, x, m+1, r, id*2+1);
t[id]=max(t[id*2], t[id*2+1]);
}
int get(int x, int y, int l=0, int r=idx+1, int id=1)
{
assert(l<=r);
if(r<x||y<l) return 0;
if(x<=l&&r<=y) return t[id];
int m=(l+r)/2;
return max(
get(x, y, l, m, id*2),
get(x, y, m+1, r, id*2+1)
);
}
signed main()
{
setIO();
cin >> n >> x;
FOR(i, 0, n)
{
cin >> a[i], st.insert(-a[i]);
}
for(auto x : st) c[x]=++idx;
vi lds;
for(int i=n-1; i>=0; i--){
int l=0, r=sz(lds);
while(l<r){
int m=(l+r)/2;
if(lds[m]>a[i]) l=m+1;
else r=m;
}
if(l==sz(lds)) lds.pb(a[i]);
else lds[l]=a[i];
nd[i]=le(lds);
d[i]=sz(lds);
}
vi lis; int ans=0;
FOR(i, 0, n-1)
{
int in=upper_bound(all(lis), a[i])-lis.begin();
if(in==sz(lis)) lis.pb(a[i]);
else lis[in]=a[i];
upd(c[-le(lis)], sz(lis));
int val=nd[i+1];
int maz=*st.upper_bound(-val-x);
int gx=get(c[maz], idx);
ans=max(ans, gx+d[i+1]);
}
cout << ans;
}
Compilation message
glo.cpp: In function 'void setIO(std::string)':
glo.cpp:32:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen((s+".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
glo.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen((s+".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
270 ms |
24688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
6604 KB |
Output is correct |
2 |
Correct |
60 ms |
6504 KB |
Output is correct |
3 |
Correct |
48 ms |
6596 KB |
Output is correct |
4 |
Incorrect |
40 ms |
4688 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
12648 KB |
Output is correct |
2 |
Correct |
109 ms |
12736 KB |
Output is correct |
3 |
Correct |
256 ms |
24772 KB |
Output is correct |
4 |
Incorrect |
191 ms |
17264 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |