#include <bits/stdc++.h>
using namespace std;
int temps[200005];
int pref_longest[200005];
int main() {
int n;
int x;
cin >> n >> x;
for (int i = 0; i < n; i++) {
cin >> temps[i];
}
vector<int> dp(n, INT_MAX);
int longest = 0;
// longest increasing subsequence ending at i
for (int i = 0; i < n; i++) {
int j = lower_bound(dp.begin(), dp.end(), temps[i]) - dp.begin();
dp[j] = temps[i];
pref_longest[i] = j + 1;
longest = max(longest, pref_longest[i]);
}
dp = vector<int>(n, INT_MAX);
// longest decreasing subsequence ending at i of reverse
// = longest increasing subsequence starting at i
for (int i = n - 1; i >= 0; i--) {
int pos = lower_bound(dp.begin(), dp.end(), -temps[i] + x)-dp.begin();
longest = max(longest, pref_longest[i] + pos);
int insert_pos = lower_bound(dp.begin(), dp.end(), -temps[i]) - dp.begin();
dp[insert_pos] = -temps[i];
}
cout << longest << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
300 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
3396 KB |
Output is correct |
2 |
Correct |
113 ms |
5252 KB |
Output is correct |
3 |
Correct |
104 ms |
5316 KB |
Output is correct |
4 |
Correct |
105 ms |
5312 KB |
Output is correct |
5 |
Correct |
73 ms |
4576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
972 KB |
Output is correct |
2 |
Correct |
25 ms |
1444 KB |
Output is correct |
3 |
Correct |
25 ms |
1484 KB |
Output is correct |
4 |
Correct |
18 ms |
1384 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
19 ms |
1324 KB |
Output is correct |
7 |
Correct |
26 ms |
1532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
1748 KB |
Output is correct |
2 |
Correct |
50 ms |
2756 KB |
Output is correct |
3 |
Correct |
104 ms |
5292 KB |
Output is correct |
4 |
Correct |
77 ms |
4728 KB |
Output is correct |
5 |
Correct |
37 ms |
2400 KB |
Output is correct |
6 |
Correct |
68 ms |
4424 KB |
Output is correct |
7 |
Correct |
86 ms |
5060 KB |
Output is correct |
8 |
Correct |
48 ms |
2784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
300 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
102 ms |
3396 KB |
Output is correct |
28 |
Correct |
113 ms |
5252 KB |
Output is correct |
29 |
Correct |
104 ms |
5316 KB |
Output is correct |
30 |
Correct |
105 ms |
5312 KB |
Output is correct |
31 |
Correct |
73 ms |
4576 KB |
Output is correct |
32 |
Correct |
25 ms |
972 KB |
Output is correct |
33 |
Correct |
25 ms |
1444 KB |
Output is correct |
34 |
Correct |
25 ms |
1484 KB |
Output is correct |
35 |
Correct |
18 ms |
1384 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
19 ms |
1324 KB |
Output is correct |
38 |
Correct |
26 ms |
1532 KB |
Output is correct |
39 |
Correct |
49 ms |
1748 KB |
Output is correct |
40 |
Correct |
50 ms |
2756 KB |
Output is correct |
41 |
Correct |
104 ms |
5292 KB |
Output is correct |
42 |
Correct |
77 ms |
4728 KB |
Output is correct |
43 |
Correct |
37 ms |
2400 KB |
Output is correct |
44 |
Correct |
68 ms |
4424 KB |
Output is correct |
45 |
Correct |
86 ms |
5060 KB |
Output is correct |
46 |
Correct |
48 ms |
2784 KB |
Output is correct |
47 |
Correct |
52 ms |
2728 KB |
Output is correct |
48 |
Correct |
52 ms |
2732 KB |
Output is correct |
49 |
Correct |
103 ms |
5280 KB |
Output is correct |
50 |
Correct |
71 ms |
4548 KB |
Output is correct |
51 |
Correct |
57 ms |
3440 KB |
Output is correct |
52 |
Correct |
76 ms |
4604 KB |
Output is correct |
53 |
Correct |
70 ms |
4612 KB |
Output is correct |
54 |
Correct |
89 ms |
5324 KB |
Output is correct |
55 |
Correct |
96 ms |
5316 KB |
Output is correct |