#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define endl '\n'
#define pb push_back
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ii = pair<int,int>;
using iii = tuple<int,int,int>;
const int inf = 2e9+1;
const int mod = 1e9+7;
const int maxn = 3e5+100;
template<typename X, typename Y> bool ckmin(X& x, const Y& y) { return (y < x) ? (x=y,1):0; }
template<typename X, typename Y> bool ckmax(X& x, const Y& y) { return (x < y) ? (x=y,1):0; }
int cmp[maxn], bit[maxn], l[maxn], n, x;
void insert(int x,int d){
for(;x<=n;x+=x&(-x))ckmax(bit[x], d);
}
int query(int x){
int ans = 0;
while(x)ckmax(ans, bit[x]), x -= x&(-x);
return ans;
}
int main(){_
//only intervals that matter are [1,i]
//if we perform the operation on [1,i], we obviously have that v[i] is on the sequence
//then, size of sequence will be LIS ending at v[i] + LIS starting at v[j] such that
//i<j and v[i] - x < v[j]
//then, precalculate left part and start calculating answer from right, inserting new possible answers in a set.
cin>>n>>x;
vector<int>v(n);
map<int,int>mp, c;
for(auto&x:v)cin>>x, mp[x] = 1;
int it = 0;
for(auto [a,b]:mp)c[a] = ++it;
for(int i=0;i<n;++i)cmp[i] = c[v[i]];
for(int i=0;i<n;++i){
l[i] = query(cmp[i]-1)+1;
insert(cmp[i], l[i]);
}
for(int i=1;i<=n;++i)bit[i] = 0;
int ans = 0;
for(int i=n-1;~i;--i){
int k = query(n-cmp[i])+1;
auto p = c.upper_bound(v[i]-x);
if(p==end(c))ckmax(ans, l[i]);
else ckmax(ans, l[i] + query(n-(*p).second+1));
insert(n-cmp[i]+1, k);
}
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 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 |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 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 |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 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 |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
352 ms |
22200 KB |
Output is correct |
2 |
Correct |
335 ms |
22220 KB |
Output is correct |
3 |
Correct |
328 ms |
22392 KB |
Output is correct |
4 |
Correct |
338 ms |
22156 KB |
Output is correct |
5 |
Correct |
93 ms |
12796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
5784 KB |
Output is correct |
2 |
Correct |
57 ms |
5796 KB |
Output is correct |
3 |
Correct |
52 ms |
5756 KB |
Output is correct |
4 |
Correct |
19 ms |
3356 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
21 ms |
3620 KB |
Output is correct |
7 |
Correct |
39 ms |
6264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
11292 KB |
Output is correct |
2 |
Correct |
102 ms |
11408 KB |
Output is correct |
3 |
Correct |
262 ms |
22140 KB |
Output is correct |
4 |
Correct |
82 ms |
12832 KB |
Output is correct |
5 |
Correct |
63 ms |
11816 KB |
Output is correct |
6 |
Correct |
125 ms |
22300 KB |
Output is correct |
7 |
Correct |
127 ms |
22988 KB |
Output is correct |
8 |
Correct |
83 ms |
12224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 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 |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 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 |
328 KB |
Output is correct |
27 |
Correct |
352 ms |
22200 KB |
Output is correct |
28 |
Correct |
335 ms |
22220 KB |
Output is correct |
29 |
Correct |
328 ms |
22392 KB |
Output is correct |
30 |
Correct |
338 ms |
22156 KB |
Output is correct |
31 |
Correct |
93 ms |
12796 KB |
Output is correct |
32 |
Correct |
53 ms |
5784 KB |
Output is correct |
33 |
Correct |
57 ms |
5796 KB |
Output is correct |
34 |
Correct |
52 ms |
5756 KB |
Output is correct |
35 |
Correct |
19 ms |
3356 KB |
Output is correct |
36 |
Correct |
1 ms |
328 KB |
Output is correct |
37 |
Correct |
21 ms |
3620 KB |
Output is correct |
38 |
Correct |
39 ms |
6264 KB |
Output is correct |
39 |
Correct |
97 ms |
11292 KB |
Output is correct |
40 |
Correct |
102 ms |
11408 KB |
Output is correct |
41 |
Correct |
262 ms |
22140 KB |
Output is correct |
42 |
Correct |
82 ms |
12832 KB |
Output is correct |
43 |
Correct |
63 ms |
11816 KB |
Output is correct |
44 |
Correct |
125 ms |
22300 KB |
Output is correct |
45 |
Correct |
127 ms |
22988 KB |
Output is correct |
46 |
Correct |
83 ms |
12224 KB |
Output is correct |
47 |
Correct |
126 ms |
12220 KB |
Output is correct |
48 |
Correct |
123 ms |
12260 KB |
Output is correct |
49 |
Correct |
341 ms |
24136 KB |
Output is correct |
50 |
Correct |
94 ms |
13904 KB |
Output is correct |
51 |
Correct |
77 ms |
12864 KB |
Output is correct |
52 |
Correct |
149 ms |
23428 KB |
Output is correct |
53 |
Correct |
127 ms |
23432 KB |
Output is correct |
54 |
Correct |
136 ms |
24164 KB |
Output is correct |
55 |
Correct |
202 ms |
24164 KB |
Output is correct |