#include <bits/stdc++.h>
using namespace std;
// I'll get used to the stage, get used to the stagin'
vector<int> dp , new_dp;
void init(int n){
dp.resize(n , 1);
new_dp.resize(n , 1);
return ;
}
int fill_dp(vector<int> v){
int n = (int)v.size();
if(n == 0) return 0;
vector<int> lis;
for(int i = 0 ; i < n ; i++){
int val = v[i];
auto q = lower_bound(lis.begin(),lis.end() , val);
int id = q - lis.begin();
if(q == lis.end()){
lis.emplace_back(val);
}
else{
*q = val;
}
dp[i] = id + 1;
}
return (int) lis.size();
}
void fill_new_dp(vector<int> v){
int n = (int)v.size();
if(n == 0) return ;
deque<int> lis;
for(int i = n - 1 ; i >= 0 ; i--){
int val = v[i];
auto q = lower_bound(lis.begin(),lis.end(),val, greater<int>());
int id = q - lis.begin();
if(q == lis.end()){
lis.emplace_back(val);
}
else *q = val;
new_dp[i] = id + 1;
}
return ;
}
template <typename T>
class fenwick {
public:
vector<T> fenw;
int n;
fenwick(int _n) : n(_n) {
fenw.resize(n);
}
void modify(int x, T v) {
while (x < n) {
fenw[x] = max(fenw[x] , v);
x |= (x + 1);
}
}
T get(int x) {
T v{};
while (x >= 0) {
v = max(v , fenw[x]);
x = (x & (x + 1)) - 1;
}
return v;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n , x;
cin >> n >> x;
init(n);
vector<int> v(n);
for(int i = 0 ; i < n ; i++){
cin >> v[i];
}
int here = fill_dp(v);
fill_new_dp(v);
if(x == 0){
cout << here << '\n';
return 0;
}
vector<int> now_t = v;
sort(now_t.begin() , now_t.end());
fenwick<int> fen(n);
int best = 0;
for(int i = n - 1 ; i >= 0 ; i--){
int val = v[i];
int idx = lower_bound(now_t.begin(),now_t.end() , val) - now_t.begin();
int id = upper_bound(now_t.begin(),now_t.end(), val - x) - now_t.begin();
if(id == n){
best = max(best , dp[i]);
continue;
}
int Max = fen.get(n - id - 1);
fen.modify(n - idx -1 , new_dp[i]);
best = max(Max + dp[i] , best);
}
cout << best << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 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 |
0 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 |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
312 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 |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
312 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 |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
312 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 |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
312 KB |
Output is correct |
19 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
5372 KB |
Output is correct |
2 |
Correct |
44 ms |
5324 KB |
Output is correct |
3 |
Correct |
45 ms |
5372 KB |
Output is correct |
4 |
Correct |
44 ms |
5340 KB |
Output is correct |
5 |
Correct |
34 ms |
5200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
1712 KB |
Output is correct |
2 |
Correct |
25 ms |
1780 KB |
Output is correct |
3 |
Correct |
26 ms |
1780 KB |
Output is correct |
4 |
Correct |
16 ms |
1584 KB |
Output is correct |
5 |
Correct |
0 ms |
312 KB |
Output is correct |
6 |
Correct |
17 ms |
1676 KB |
Output is correct |
7 |
Correct |
21 ms |
1800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
3116 KB |
Output is correct |
2 |
Correct |
45 ms |
3108 KB |
Output is correct |
3 |
Correct |
93 ms |
6124 KB |
Output is correct |
4 |
Correct |
57 ms |
5284 KB |
Output is correct |
5 |
Correct |
26 ms |
2844 KB |
Output is correct |
6 |
Correct |
61 ms |
5316 KB |
Output is correct |
7 |
Correct |
50 ms |
5868 KB |
Output is correct |
8 |
Correct |
36 ms |
3236 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 |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
312 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 |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
312 KB |
Output is correct |
19 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
45 ms |
5372 KB |
Output is correct |
28 |
Correct |
44 ms |
5324 KB |
Output is correct |
29 |
Correct |
45 ms |
5372 KB |
Output is correct |
30 |
Correct |
44 ms |
5340 KB |
Output is correct |
31 |
Correct |
34 ms |
5200 KB |
Output is correct |
32 |
Correct |
25 ms |
1712 KB |
Output is correct |
33 |
Correct |
25 ms |
1780 KB |
Output is correct |
34 |
Correct |
26 ms |
1780 KB |
Output is correct |
35 |
Correct |
16 ms |
1584 KB |
Output is correct |
36 |
Correct |
0 ms |
312 KB |
Output is correct |
37 |
Correct |
17 ms |
1676 KB |
Output is correct |
38 |
Correct |
21 ms |
1800 KB |
Output is correct |
39 |
Correct |
45 ms |
3116 KB |
Output is correct |
40 |
Correct |
45 ms |
3108 KB |
Output is correct |
41 |
Correct |
93 ms |
6124 KB |
Output is correct |
42 |
Correct |
57 ms |
5284 KB |
Output is correct |
43 |
Correct |
26 ms |
2844 KB |
Output is correct |
44 |
Correct |
61 ms |
5316 KB |
Output is correct |
45 |
Correct |
50 ms |
5868 KB |
Output is correct |
46 |
Correct |
36 ms |
3236 KB |
Output is correct |
47 |
Correct |
54 ms |
3228 KB |
Output is correct |
48 |
Correct |
55 ms |
3132 KB |
Output is correct |
49 |
Correct |
111 ms |
6140 KB |
Output is correct |
50 |
Correct |
67 ms |
5284 KB |
Output is correct |
51 |
Correct |
52 ms |
4128 KB |
Output is correct |
52 |
Correct |
70 ms |
5504 KB |
Output is correct |
53 |
Correct |
45 ms |
5428 KB |
Output is correct |
54 |
Correct |
52 ms |
6112 KB |
Output is correct |
55 |
Correct |
90 ms |
6176 KB |
Output is correct |