#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <utility>
#include <cmath>
#include <numeric>
#include <assert.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 300007;
const ll inf = 1e9;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int n, x, a[M], pref[M], suf[M], last[M], seg[32 * M], L[32 * M], R[32 * M], ans, nodesNum = 1;
map <int,int> lastocur;
void upd(int pos, int v, int node = 1, int l = 1, int r = 1e9 + 1)
{
if(l == r){
seg[node] = v;
return;
}
int mid = (l + r) / 2;
if(pos <= mid){
if(!L[node]) L[node] = ++nodesNum;
upd(pos, v, L[node], l, mid);
}
else{
if(!R[node]) R[node] = ++nodesNum;
upd(pos, v, R[node], mid + 1, r);
}
seg[node] = max(seg[L[node]], seg[R[node]]);
return;
}
int get(int st, int en, int node = 1, int l = 1, int r = 1e9 + 1)
{
if(l > en || r < st) return -inf;
if(l >= st && r <= en) return seg[node];
if(!L[node]) L[node] = ++nodesNum;
if(!R[node]) R[node] = ++nodesNum;
int mid = (l + r) / 2;
return max(get(st, en, L[node], l, mid), get(st, en, R[node], mid + 1, r));
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> x;
for(int i = 1; i <= n; ++i) cin >> a[i];
vector <int> lis;
for(int i = 1; i <= n; ++i){
int it = lower_bound(all(lis), a[i]) - lis.begin();
if(it == lis.size()) lis.pb(a[i]);
else lis[it] = a[i];
pref[i] = it + 1;
}
lis.clear();
for(int i = n; i; --i){
int it = lower_bound(all(lis), - a[i]) - lis.begin();
if(it == lis.size()) lis.pb(-a[i]);
else lis[it] = -a[i];
suf[i] = it + 1;
upd(a[i], it + 1);
if(lastocur[a[i]]) last[i] = lastocur[a[i]];
lastocur[a[i]] = i;
}
for(int i = 1; i <= n; ++i){
upd(a[i], suf[last[i]]);
ans = max(ans, pref[i] + get(max(1, a[i] - x + 1), 1e9 + 1));
}
cout << ans << endl;
return 0;
}
Compilation message
glo.cpp: In function 'int main()':
glo.cpp:68:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | if(it == lis.size()) lis.pb(a[i]);
| ~~~^~~~~~~~~~~~~
glo.cpp:75:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | if(it == lis.size()) lis.pb(-a[i]);
| ~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
844 KB |
Output is correct |
20 |
Correct |
2 ms |
844 KB |
Output is correct |
21 |
Correct |
2 ms |
844 KB |
Output is correct |
22 |
Correct |
2 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
526 ms |
43712 KB |
Output is correct |
2 |
Correct |
549 ms |
43852 KB |
Output is correct |
3 |
Correct |
587 ms |
43832 KB |
Output is correct |
4 |
Correct |
563 ms |
43652 KB |
Output is correct |
5 |
Correct |
226 ms |
25584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
106 ms |
17988 KB |
Output is correct |
2 |
Correct |
101 ms |
18052 KB |
Output is correct |
3 |
Correct |
106 ms |
17980 KB |
Output is correct |
4 |
Correct |
50 ms |
10208 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
50 ms |
3268 KB |
Output is correct |
7 |
Correct |
78 ms |
13424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
188 ms |
23164 KB |
Output is correct |
2 |
Correct |
188 ms |
23108 KB |
Output is correct |
3 |
Correct |
492 ms |
43716 KB |
Output is correct |
4 |
Correct |
168 ms |
25436 KB |
Output is correct |
5 |
Correct |
80 ms |
8708 KB |
Output is correct |
6 |
Correct |
157 ms |
16300 KB |
Output is correct |
7 |
Correct |
161 ms |
16272 KB |
Output is correct |
8 |
Correct |
119 ms |
18300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
844 KB |
Output is correct |
20 |
Correct |
2 ms |
844 KB |
Output is correct |
21 |
Correct |
2 ms |
844 KB |
Output is correct |
22 |
Correct |
2 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
526 ms |
43712 KB |
Output is correct |
28 |
Correct |
549 ms |
43852 KB |
Output is correct |
29 |
Correct |
587 ms |
43832 KB |
Output is correct |
30 |
Correct |
563 ms |
43652 KB |
Output is correct |
31 |
Correct |
226 ms |
25584 KB |
Output is correct |
32 |
Correct |
106 ms |
17988 KB |
Output is correct |
33 |
Correct |
101 ms |
18052 KB |
Output is correct |
34 |
Correct |
106 ms |
17980 KB |
Output is correct |
35 |
Correct |
50 ms |
10208 KB |
Output is correct |
36 |
Correct |
0 ms |
332 KB |
Output is correct |
37 |
Correct |
50 ms |
3268 KB |
Output is correct |
38 |
Correct |
78 ms |
13424 KB |
Output is correct |
39 |
Correct |
188 ms |
23164 KB |
Output is correct |
40 |
Correct |
188 ms |
23108 KB |
Output is correct |
41 |
Correct |
492 ms |
43716 KB |
Output is correct |
42 |
Correct |
168 ms |
25436 KB |
Output is correct |
43 |
Correct |
80 ms |
8708 KB |
Output is correct |
44 |
Correct |
157 ms |
16300 KB |
Output is correct |
45 |
Correct |
161 ms |
16272 KB |
Output is correct |
46 |
Correct |
119 ms |
18300 KB |
Output is correct |
47 |
Correct |
268 ms |
37188 KB |
Output is correct |
48 |
Correct |
258 ms |
39360 KB |
Output is correct |
49 |
Correct |
623 ms |
75044 KB |
Output is correct |
50 |
Correct |
210 ms |
41716 KB |
Output is correct |
51 |
Correct |
145 ms |
9656 KB |
Output is correct |
52 |
Correct |
219 ms |
17084 KB |
Output is correct |
53 |
Correct |
158 ms |
17164 KB |
Output is correct |
54 |
Correct |
160 ms |
17168 KB |
Output is correct |
55 |
Correct |
380 ms |
54860 KB |
Output is correct |