#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define f first
#define s second
const int N = 2e5 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e9 + 100;
int dp[N], a[N], l[N], r[N], n, x, seg[4*N];
vector<int> v;
int get(int L, int R, int b = 0, int e = n, int ind = 1)
{
if(R <= b || e <= L)
return 0;
if(L <= b && e <= R)
return seg[ind];
int mid = (b + e) / 2;
return max(get(L, R, mid, e, ind*2+1), get(L, R, b, mid, ind*2));
}
void add(int k, int val, int b = 0, int e = n, int ind = 1)
{
if(b + 1 == e)
{
seg[ind] = val;
return;
}
int mid = (b + e) / 2;
if(k < mid)
add(k, val, b, mid, ind*2);
else
add(k, val, mid, e, ind*2+1);
seg[ind] = max(seg[ind*2], seg[ind*2+1]);
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> x;
for(int i = 0; i < n; i++)
{
cin >> a[i];
v.push_back(a[i]);
}
sort(v.begin(), v.end());
int p = 1;
for(int i = 0; i < n; i++)
{
int j = lower_bound(dp, dp+p, a[i]) - dp;
dp[j] = a[i];
p = max(p, j+1);
l[i] = j;
}
//cout << p - 1 << endl;
for(int i = 0; i < n; i++)
dp[i] = -inf;
p = 1;
for(int i = n-1; i >= 0; i--)
{
int j = lower_bound(dp, dp+p, -a[i]) - dp;
dp[j] = -a[i];
p = max(p, j+1);
r[i] = j;
//cout << r[i] << endl;
}
int ans = 1;
for(int i = 0; i < n; i++)
{
int id = lower_bound(v.begin(), v.end(), a[i]+x) - v.begin();
int mx = get(0, id);
ans = max(ans, r[i] + mx);
id = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
add(id, l[i]);
}
cout << ans << endl;
return 0;
}
/*
8 10
7 3 5 12 2 7 3 4
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
2 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
224 ms |
6644 KB |
Output is correct |
2 |
Correct |
228 ms |
8372 KB |
Output is correct |
3 |
Correct |
222 ms |
8432 KB |
Output is correct |
4 |
Correct |
223 ms |
8424 KB |
Output is correct |
5 |
Correct |
114 ms |
6632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
2032 KB |
Output is correct |
2 |
Correct |
48 ms |
2416 KB |
Output is correct |
3 |
Correct |
48 ms |
2544 KB |
Output is correct |
4 |
Correct |
24 ms |
2032 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
30 ms |
2288 KB |
Output is correct |
7 |
Correct |
41 ms |
2544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
3436 KB |
Output is correct |
2 |
Correct |
75 ms |
4588 KB |
Output is correct |
3 |
Correct |
158 ms |
8424 KB |
Output is correct |
4 |
Correct |
81 ms |
6836 KB |
Output is correct |
5 |
Correct |
42 ms |
4076 KB |
Output is correct |
6 |
Correct |
76 ms |
7400 KB |
Output is correct |
7 |
Correct |
87 ms |
8168 KB |
Output is correct |
8 |
Correct |
59 ms |
4460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
2 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
224 ms |
6644 KB |
Output is correct |
28 |
Correct |
228 ms |
8372 KB |
Output is correct |
29 |
Correct |
222 ms |
8432 KB |
Output is correct |
30 |
Correct |
223 ms |
8424 KB |
Output is correct |
31 |
Correct |
114 ms |
6632 KB |
Output is correct |
32 |
Correct |
47 ms |
2032 KB |
Output is correct |
33 |
Correct |
48 ms |
2416 KB |
Output is correct |
34 |
Correct |
48 ms |
2544 KB |
Output is correct |
35 |
Correct |
24 ms |
2032 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Correct |
30 ms |
2288 KB |
Output is correct |
38 |
Correct |
41 ms |
2544 KB |
Output is correct |
39 |
Correct |
73 ms |
3436 KB |
Output is correct |
40 |
Correct |
75 ms |
4588 KB |
Output is correct |
41 |
Correct |
158 ms |
8424 KB |
Output is correct |
42 |
Correct |
81 ms |
6836 KB |
Output is correct |
43 |
Correct |
42 ms |
4076 KB |
Output is correct |
44 |
Correct |
76 ms |
7400 KB |
Output is correct |
45 |
Correct |
87 ms |
8168 KB |
Output is correct |
46 |
Correct |
59 ms |
4460 KB |
Output is correct |
47 |
Correct |
105 ms |
4460 KB |
Output is correct |
48 |
Correct |
104 ms |
4588 KB |
Output is correct |
49 |
Correct |
226 ms |
8536 KB |
Output is correct |
50 |
Correct |
107 ms |
6760 KB |
Output is correct |
51 |
Correct |
105 ms |
6376 KB |
Output is correct |
52 |
Correct |
141 ms |
7784 KB |
Output is correct |
53 |
Correct |
80 ms |
7876 KB |
Output is correct |
54 |
Correct |
84 ms |
8424 KB |
Output is correct |
55 |
Correct |
184 ms |
8424 KB |
Output is correct |