#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> pbds;
int a[222222];
int P[222222];
int S[222222];
struct Fenwick
{
vi fen;
int n;
void init(int _n)
{
n=_n; fen.clear();
fen.resize(n+1,0);
}
void update(int pos, int v)
{
pos++;
for(;pos<=n;pos+=(pos&(-pos)))
{
fen[pos]=max(fen[pos],v);
}
}
int get(int pos)
{
if(pos<0) return 0;
pos++;
int ans=0;
for(;pos;pos-=(pos&(-pos)))
{
ans=max(ans,fen[pos]);
}
return ans;
}
};
vi coord;
int id[222222];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int n, x; cin>>n>>x;
for(int i=0;i<n;i++)
{
cin>>a[i]; coord.pb(a[i]);
}
sort(coord.begin(),coord.end()); coord.erase(unique(coord.begin(),coord.end()),coord.end());
for(int i=0;i<n;i++)
{
id[i] = lower_bound(coord.begin(),coord.end(),a[i])-coord.begin();
}
Fenwick fen; fen.init(n+1);
for(int i=0;i<n;i++)
{
P[i] = fen.get(id[i]-1)+1;
fen.update(id[i],P[i]);
}
fen.init(n+1);
for(int i=n-1;i>=0;i--)
{
S[i] = fen.get(n-id[i]-1)+1;
fen.update(n-id[i],S[i]);
}
int ans=P[n-1];
Fenwick fen2; fen2.init(n+1);
for(int j=0;j<n;j++)
{
int lb = lower_bound(coord.begin(),coord.end(),a[j]+x)-coord.begin();
ans=max(ans, S[j]+fen2.get(lb-1));
fen2.update(id[j],P[j]);
}
cout<<ans<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
420 KB |
Output is correct |
4 |
Correct |
2 ms |
420 KB |
Output is correct |
5 |
Correct |
2 ms |
420 KB |
Output is correct |
6 |
Correct |
2 ms |
496 KB |
Output is correct |
7 |
Correct |
2 ms |
572 KB |
Output is correct |
8 |
Correct |
3 ms |
572 KB |
Output is correct |
9 |
Correct |
2 ms |
572 KB |
Output is correct |
10 |
Correct |
3 ms |
572 KB |
Output is correct |
11 |
Correct |
2 ms |
616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
420 KB |
Output is correct |
4 |
Correct |
2 ms |
420 KB |
Output is correct |
5 |
Correct |
2 ms |
420 KB |
Output is correct |
6 |
Correct |
2 ms |
496 KB |
Output is correct |
7 |
Correct |
2 ms |
572 KB |
Output is correct |
8 |
Correct |
3 ms |
572 KB |
Output is correct |
9 |
Correct |
2 ms |
572 KB |
Output is correct |
10 |
Correct |
3 ms |
572 KB |
Output is correct |
11 |
Correct |
2 ms |
616 KB |
Output is correct |
12 |
Correct |
3 ms |
616 KB |
Output is correct |
13 |
Correct |
2 ms |
616 KB |
Output is correct |
14 |
Correct |
2 ms |
616 KB |
Output is correct |
15 |
Correct |
3 ms |
616 KB |
Output is correct |
16 |
Correct |
3 ms |
744 KB |
Output is correct |
17 |
Correct |
3 ms |
744 KB |
Output is correct |
18 |
Correct |
3 ms |
744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
420 KB |
Output is correct |
4 |
Correct |
2 ms |
420 KB |
Output is correct |
5 |
Correct |
2 ms |
420 KB |
Output is correct |
6 |
Correct |
2 ms |
496 KB |
Output is correct |
7 |
Correct |
2 ms |
572 KB |
Output is correct |
8 |
Correct |
3 ms |
572 KB |
Output is correct |
9 |
Correct |
2 ms |
572 KB |
Output is correct |
10 |
Correct |
3 ms |
572 KB |
Output is correct |
11 |
Correct |
2 ms |
616 KB |
Output is correct |
12 |
Correct |
3 ms |
616 KB |
Output is correct |
13 |
Correct |
2 ms |
616 KB |
Output is correct |
14 |
Correct |
2 ms |
616 KB |
Output is correct |
15 |
Correct |
3 ms |
616 KB |
Output is correct |
16 |
Correct |
3 ms |
744 KB |
Output is correct |
17 |
Correct |
3 ms |
744 KB |
Output is correct |
18 |
Correct |
3 ms |
744 KB |
Output is correct |
19 |
Correct |
3 ms |
744 KB |
Output is correct |
20 |
Correct |
2 ms |
744 KB |
Output is correct |
21 |
Correct |
3 ms |
744 KB |
Output is correct |
22 |
Correct |
3 ms |
744 KB |
Output is correct |
23 |
Correct |
3 ms |
744 KB |
Output is correct |
24 |
Correct |
2 ms |
744 KB |
Output is correct |
25 |
Correct |
2 ms |
744 KB |
Output is correct |
26 |
Correct |
3 ms |
744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
165 ms |
6260 KB |
Output is correct |
2 |
Correct |
157 ms |
6288 KB |
Output is correct |
3 |
Correct |
181 ms |
6288 KB |
Output is correct |
4 |
Correct |
165 ms |
6288 KB |
Output is correct |
5 |
Correct |
124 ms |
6288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
6288 KB |
Output is correct |
2 |
Correct |
36 ms |
6288 KB |
Output is correct |
3 |
Correct |
33 ms |
6288 KB |
Output is correct |
4 |
Correct |
22 ms |
6288 KB |
Output is correct |
5 |
Correct |
2 ms |
6288 KB |
Output is correct |
6 |
Correct |
20 ms |
6288 KB |
Output is correct |
7 |
Correct |
31 ms |
6288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
6288 KB |
Output is correct |
2 |
Correct |
61 ms |
6288 KB |
Output is correct |
3 |
Correct |
130 ms |
6288 KB |
Output is correct |
4 |
Correct |
76 ms |
6288 KB |
Output is correct |
5 |
Correct |
40 ms |
6288 KB |
Output is correct |
6 |
Correct |
71 ms |
6288 KB |
Output is correct |
7 |
Correct |
81 ms |
6288 KB |
Output is correct |
8 |
Correct |
53 ms |
6288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
420 KB |
Output is correct |
4 |
Correct |
2 ms |
420 KB |
Output is correct |
5 |
Correct |
2 ms |
420 KB |
Output is correct |
6 |
Correct |
2 ms |
496 KB |
Output is correct |
7 |
Correct |
2 ms |
572 KB |
Output is correct |
8 |
Correct |
3 ms |
572 KB |
Output is correct |
9 |
Correct |
2 ms |
572 KB |
Output is correct |
10 |
Correct |
3 ms |
572 KB |
Output is correct |
11 |
Correct |
2 ms |
616 KB |
Output is correct |
12 |
Correct |
3 ms |
616 KB |
Output is correct |
13 |
Correct |
2 ms |
616 KB |
Output is correct |
14 |
Correct |
2 ms |
616 KB |
Output is correct |
15 |
Correct |
3 ms |
616 KB |
Output is correct |
16 |
Correct |
3 ms |
744 KB |
Output is correct |
17 |
Correct |
3 ms |
744 KB |
Output is correct |
18 |
Correct |
3 ms |
744 KB |
Output is correct |
19 |
Correct |
3 ms |
744 KB |
Output is correct |
20 |
Correct |
2 ms |
744 KB |
Output is correct |
21 |
Correct |
3 ms |
744 KB |
Output is correct |
22 |
Correct |
3 ms |
744 KB |
Output is correct |
23 |
Correct |
3 ms |
744 KB |
Output is correct |
24 |
Correct |
2 ms |
744 KB |
Output is correct |
25 |
Correct |
2 ms |
744 KB |
Output is correct |
26 |
Correct |
3 ms |
744 KB |
Output is correct |
27 |
Correct |
165 ms |
6260 KB |
Output is correct |
28 |
Correct |
157 ms |
6288 KB |
Output is correct |
29 |
Correct |
181 ms |
6288 KB |
Output is correct |
30 |
Correct |
165 ms |
6288 KB |
Output is correct |
31 |
Correct |
124 ms |
6288 KB |
Output is correct |
32 |
Correct |
34 ms |
6288 KB |
Output is correct |
33 |
Correct |
36 ms |
6288 KB |
Output is correct |
34 |
Correct |
33 ms |
6288 KB |
Output is correct |
35 |
Correct |
22 ms |
6288 KB |
Output is correct |
36 |
Correct |
2 ms |
6288 KB |
Output is correct |
37 |
Correct |
20 ms |
6288 KB |
Output is correct |
38 |
Correct |
31 ms |
6288 KB |
Output is correct |
39 |
Correct |
58 ms |
6288 KB |
Output is correct |
40 |
Correct |
61 ms |
6288 KB |
Output is correct |
41 |
Correct |
130 ms |
6288 KB |
Output is correct |
42 |
Correct |
76 ms |
6288 KB |
Output is correct |
43 |
Correct |
40 ms |
6288 KB |
Output is correct |
44 |
Correct |
71 ms |
6288 KB |
Output is correct |
45 |
Correct |
81 ms |
6288 KB |
Output is correct |
46 |
Correct |
53 ms |
6288 KB |
Output is correct |
47 |
Correct |
75 ms |
6288 KB |
Output is correct |
48 |
Correct |
84 ms |
6288 KB |
Output is correct |
49 |
Correct |
161 ms |
6288 KB |
Output is correct |
50 |
Correct |
81 ms |
6288 KB |
Output is correct |
51 |
Correct |
67 ms |
6288 KB |
Output is correct |
52 |
Correct |
99 ms |
6312 KB |
Output is correct |
53 |
Correct |
79 ms |
6312 KB |
Output is correct |
54 |
Correct |
95 ms |
6312 KB |
Output is correct |
55 |
Correct |
151 ms |
6372 KB |
Output is correct |