#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
const int N = 2e5 + 50;
int n, m, s[N], f[N];
int x, t[N];
vector<int> dp;
int a[N];
int fen[N];
int get(int i) {
int out = fen[0];
for (; i > 0; i -= i&(-i)) {
out = max(out, fen[i]);
}
return out;
}
void upd(int i, int x) {
if (!i) fen[i] = max(fen[i], x);
else for (; i <= n; i += i&(-i)) {
fen[i] = max(fen[i], x);
}
}
void solve() {
cin >> n >> x;
for (int i = 1; i <= n; i++) {
cin >> t[i];
a[i] = t[i];
}
sort(a+1, a+n+1);
m = unique(a+1, a+n+1)-a-1;
for (int i = 1; i <= n; i++) {
int lb = lower_bound(dp.begin(), dp.end(), t[i])-dp.begin();
if (lb >= (int)dp.size()) dp.push_back(t[i]);
dp[lb] = t[i];
f[i] = lb+1;
}
for (int i = 1; i <= n; i++) {
t[i] = -t[i];
}
dp.clear();
for (int i = n; i >= 1; i--) {
int lb = lower_bound(dp.begin(), dp.end(), t[i])-dp.begin();
if (lb >= (int)dp.size()) dp.push_back(t[i]);
dp[lb] = t[i];
s[i] = lb+1;
}
for (int i = 1; i <= n; i++) {
t[i] = -t[i];
}
int ans = 0;
for (int i = 1; i <= n; i++) {
int id = lower_bound(a+1, a+m+1, t[i]+x)-a-1;
ans = max(ans, s[i] + get(id));
int id2 = lower_bound(a+1, a+m+1, t[i])-a;
upd(id2, f[i]);
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
6144 KB |
Output is correct |
2 |
Correct |
112 ms |
6148 KB |
Output is correct |
3 |
Correct |
108 ms |
6200 KB |
Output is correct |
4 |
Correct |
111 ms |
6092 KB |
Output is correct |
5 |
Correct |
53 ms |
5452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
1708 KB |
Output is correct |
2 |
Correct |
24 ms |
1748 KB |
Output is correct |
3 |
Correct |
24 ms |
1764 KB |
Output is correct |
4 |
Correct |
13 ms |
1748 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
13 ms |
1620 KB |
Output is correct |
7 |
Correct |
22 ms |
1756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
3148 KB |
Output is correct |
2 |
Correct |
45 ms |
3256 KB |
Output is correct |
3 |
Correct |
93 ms |
6268 KB |
Output is correct |
4 |
Correct |
50 ms |
5488 KB |
Output is correct |
5 |
Correct |
30 ms |
3196 KB |
Output is correct |
6 |
Correct |
47 ms |
5644 KB |
Output is correct |
7 |
Correct |
49 ms |
6356 KB |
Output is correct |
8 |
Correct |
37 ms |
3248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
108 ms |
6144 KB |
Output is correct |
28 |
Correct |
112 ms |
6148 KB |
Output is correct |
29 |
Correct |
108 ms |
6200 KB |
Output is correct |
30 |
Correct |
111 ms |
6092 KB |
Output is correct |
31 |
Correct |
53 ms |
5452 KB |
Output is correct |
32 |
Correct |
24 ms |
1708 KB |
Output is correct |
33 |
Correct |
24 ms |
1748 KB |
Output is correct |
34 |
Correct |
24 ms |
1764 KB |
Output is correct |
35 |
Correct |
13 ms |
1748 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
13 ms |
1620 KB |
Output is correct |
38 |
Correct |
22 ms |
1756 KB |
Output is correct |
39 |
Correct |
44 ms |
3148 KB |
Output is correct |
40 |
Correct |
45 ms |
3256 KB |
Output is correct |
41 |
Correct |
93 ms |
6268 KB |
Output is correct |
42 |
Correct |
50 ms |
5488 KB |
Output is correct |
43 |
Correct |
30 ms |
3196 KB |
Output is correct |
44 |
Correct |
47 ms |
5644 KB |
Output is correct |
45 |
Correct |
49 ms |
6356 KB |
Output is correct |
46 |
Correct |
37 ms |
3248 KB |
Output is correct |
47 |
Correct |
50 ms |
3260 KB |
Output is correct |
48 |
Correct |
51 ms |
3148 KB |
Output is correct |
49 |
Correct |
109 ms |
6076 KB |
Output is correct |
50 |
Correct |
52 ms |
5524 KB |
Output is correct |
51 |
Correct |
48 ms |
4440 KB |
Output is correct |
52 |
Correct |
61 ms |
5900 KB |
Output is correct |
53 |
Correct |
44 ms |
5900 KB |
Output is correct |
54 |
Correct |
48 ms |
6616 KB |
Output is correct |
55 |
Correct |
87 ms |
6136 KB |
Output is correct |