#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> pi;
#define f first
#define s second
#define FAST ios_base::sync_with_stdio(0); cin.tie(0);
#define all(x) x.begin(),x.end()
typedef pair<int, pi> pii;
const int maxn = 200010;
const int INF = LLONG_MAX/2;
const int mod = 1e9+7;
int n, X;
struct node {
int s,m,e,v;
node *l, *r;
node (int _s, int _e) {
s = _s; e = _e; m = (s+e)/2;
v = 0;
if (s != e) {
l = new node(s,m);
r = new node(m+1,e);
}
}
void up(int x, int newv) {
if (s == e) v = max(v,newv);
else {
if (x <= m) l -> up(x,newv);
else r -> up(x,newv);
v = max(l -> v, r -> v);
}
}
int rmq(int x, int y) {
if (x == s and e == y) return v;
if (x > m) return r -> rmq(x,y);
else if (y <= m) return l -> rmq(x,y);
else return max(l -> rmq(x,m), r -> rmq(m+1,y));
}
}*root;
int best[maxn];
int suf;
int32_t main() {
//FAST
cin >> n >> X;
vector <int> v(n);
for (int i =0;i<n;i++) {
cin >> v[i];
}
vector <int> vv = v;
sort(all(vv));
root = new node(0,n-1);
vector <int> lis;
for (int i =0;i<n;i++) {
int x = v[i];
if (lis.empty() or x > lis.back()) {
lis.push_back(x);
best[i] = lis.size();
} else {
int pos = lower_bound(all(lis),x) - lis.begin();
lis[pos] = x;
best[i] = pos + 1;
}
}
//cout << "HELLO";
//for (int i =0;i<n;i++) cout << best[i] << " ";
int rans = best[n-1];
for (int i =n-1;i>=0;i--) {
int p = upper_bound(all(vv),v[i] - X) - vv.begin();
if (p <= n-1) suf = root -> rmq(p,n-1);
else suf = 0;
rans = max(rans, best[i] + suf);
p = upper_bound(all(vv),v[i]) - vv.begin();
if (p <= n-1) suf = root -> rmq(p,n-1);
else suf = 0;
int curp = lower_bound(all(vv),v[i]) - vv.begin();
root -> up(curp,suf+1);
}
cout << rans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
2 ms |
364 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 |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
2 ms |
364 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 |
492 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 |
2 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
2 ms |
364 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 |
492 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 |
2 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
2 ms |
492 KB |
Output is correct |
20 |
Correct |
2 ms |
492 KB |
Output is correct |
21 |
Correct |
2 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
492 KB |
Output is correct |
23 |
Correct |
2 ms |
492 KB |
Output is correct |
24 |
Correct |
2 ms |
492 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
452 ms |
30132 KB |
Output is correct |
2 |
Correct |
434 ms |
30060 KB |
Output is correct |
3 |
Correct |
448 ms |
30188 KB |
Output is correct |
4 |
Correct |
453 ms |
30232 KB |
Output is correct |
5 |
Correct |
184 ms |
30944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
7916 KB |
Output is correct |
2 |
Correct |
99 ms |
7788 KB |
Output is correct |
3 |
Correct |
91 ms |
7788 KB |
Output is correct |
4 |
Correct |
44 ms |
8044 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
48 ms |
8444 KB |
Output is correct |
7 |
Correct |
75 ms |
8324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
186 ms |
15212 KB |
Output is correct |
2 |
Correct |
197 ms |
15340 KB |
Output is correct |
3 |
Correct |
403 ms |
30216 KB |
Output is correct |
4 |
Correct |
171 ms |
30944 KB |
Output is correct |
5 |
Correct |
94 ms |
16360 KB |
Output is correct |
6 |
Correct |
170 ms |
30692 KB |
Output is correct |
7 |
Correct |
191 ms |
31432 KB |
Output is correct |
8 |
Correct |
134 ms |
16364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
2 ms |
364 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 |
492 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 |
2 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
2 ms |
492 KB |
Output is correct |
20 |
Correct |
2 ms |
492 KB |
Output is correct |
21 |
Correct |
2 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
492 KB |
Output is correct |
23 |
Correct |
2 ms |
492 KB |
Output is correct |
24 |
Correct |
2 ms |
492 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
492 KB |
Output is correct |
27 |
Correct |
452 ms |
30132 KB |
Output is correct |
28 |
Correct |
434 ms |
30060 KB |
Output is correct |
29 |
Correct |
448 ms |
30188 KB |
Output is correct |
30 |
Correct |
453 ms |
30232 KB |
Output is correct |
31 |
Correct |
184 ms |
30944 KB |
Output is correct |
32 |
Correct |
91 ms |
7916 KB |
Output is correct |
33 |
Correct |
99 ms |
7788 KB |
Output is correct |
34 |
Correct |
91 ms |
7788 KB |
Output is correct |
35 |
Correct |
44 ms |
8044 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Correct |
48 ms |
8444 KB |
Output is correct |
38 |
Correct |
75 ms |
8324 KB |
Output is correct |
39 |
Correct |
186 ms |
15212 KB |
Output is correct |
40 |
Correct |
197 ms |
15340 KB |
Output is correct |
41 |
Correct |
403 ms |
30216 KB |
Output is correct |
42 |
Correct |
171 ms |
30944 KB |
Output is correct |
43 |
Correct |
94 ms |
16360 KB |
Output is correct |
44 |
Correct |
170 ms |
30692 KB |
Output is correct |
45 |
Correct |
191 ms |
31432 KB |
Output is correct |
46 |
Correct |
134 ms |
16364 KB |
Output is correct |
47 |
Correct |
207 ms |
16248 KB |
Output is correct |
48 |
Correct |
191 ms |
16236 KB |
Output is correct |
49 |
Correct |
443 ms |
32072 KB |
Output is correct |
50 |
Correct |
195 ms |
32096 KB |
Output is correct |
51 |
Correct |
166 ms |
24440 KB |
Output is correct |
52 |
Correct |
234 ms |
32396 KB |
Output is correct |
53 |
Correct |
187 ms |
32224 KB |
Output is correct |
54 |
Correct |
219 ms |
32992 KB |
Output is correct |
55 |
Correct |
344 ms |
32264 KB |
Output is correct |