#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
const int N = 2e5+10;
const int INF = INT_MAX;
const int mod = 1e9+7;
ll a[N],n,r[N],bit[4*N];
ll m;
vector<ll> pos = {};
ll b[N],ans,res;
void pre_calc() {
for(int i = 0 ; i <= n ; i++) {
pos.push_back(a[i]+1);
pos.push_back(a[i]);
pos.push_back(a[i]-m+1);
}
sort(pos.begin(),pos.end());
pos.resize(unique(pos.begin(),pos.end())-pos.begin());
}
ll get_val(ll x) {
return lower_bound(pos.begin(),pos.end(),x)-pos.begin()+1;
}
void update(ll id, ll val) {
while(id > 0) {
bit[id] = max(bit[id],val);
id -= (id & (-id));
}
}
ll get(ll id) {
ll res = 0;
while(id <= 3*N) {
res = max(res,bit[id]);
id += (id & (-id));
}
return res;
}
signed main()
{
if (fopen("GLO.inp", "r")) {
freopen("GLO.inp", "r", stdin);
freopen("GLO.out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1 ; i <= n ; i++) {
cin >> a[i];
}
pre_calc();
for(int i = n ; i >= 1 ; i--) {
ll id = get_val(a[i]-m+1);
r[i] = get(id)+1;
ll id2 = get_val(a[i]);
id = get_val(a[i]+1);
update(id2,get(id)+1);
ans = max(ans,r[i]);
}
for(int i = 1 ; i <= n ; i++) {
ll id = lower_bound(b+1,b+res+1,a[i])-b;
b[id] = a[i];
res = max(res,id);
ans = max(ans,id+r[i]-1);
ans = max(ans,res);
}
cout << ans;
return 0;
}
Compilation message
glo.cpp: In function 'int main()':
glo.cpp:49:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | freopen("GLO.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
glo.cpp:50:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | freopen("GLO.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6644 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6620 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6644 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6620 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6616 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6516 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6644 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6620 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6616 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6516 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6744 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6624 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
143 ms |
17524 KB |
Output is correct |
2 |
Correct |
140 ms |
17736 KB |
Output is correct |
3 |
Correct |
147 ms |
16824 KB |
Output is correct |
4 |
Correct |
148 ms |
17080 KB |
Output is correct |
5 |
Correct |
67 ms |
15452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
8916 KB |
Output is correct |
2 |
Correct |
33 ms |
9380 KB |
Output is correct |
3 |
Correct |
34 ms |
8940 KB |
Output is correct |
4 |
Correct |
19 ms |
8404 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
16 ms |
8144 KB |
Output is correct |
7 |
Correct |
26 ms |
8656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
12244 KB |
Output is correct |
2 |
Correct |
77 ms |
12412 KB |
Output is correct |
3 |
Correct |
159 ms |
17268 KB |
Output is correct |
4 |
Correct |
71 ms |
15568 KB |
Output is correct |
5 |
Correct |
42 ms |
10940 KB |
Output is correct |
6 |
Correct |
70 ms |
16080 KB |
Output is correct |
7 |
Correct |
80 ms |
15556 KB |
Output is correct |
8 |
Correct |
58 ms |
11228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6644 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6620 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6616 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6516 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6744 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6624 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
143 ms |
17524 KB |
Output is correct |
28 |
Correct |
140 ms |
17736 KB |
Output is correct |
29 |
Correct |
147 ms |
16824 KB |
Output is correct |
30 |
Correct |
148 ms |
17080 KB |
Output is correct |
31 |
Correct |
67 ms |
15452 KB |
Output is correct |
32 |
Correct |
35 ms |
8916 KB |
Output is correct |
33 |
Correct |
33 ms |
9380 KB |
Output is correct |
34 |
Correct |
34 ms |
8940 KB |
Output is correct |
35 |
Correct |
19 ms |
8404 KB |
Output is correct |
36 |
Correct |
1 ms |
6492 KB |
Output is correct |
37 |
Correct |
16 ms |
8144 KB |
Output is correct |
38 |
Correct |
26 ms |
8656 KB |
Output is correct |
39 |
Correct |
74 ms |
12244 KB |
Output is correct |
40 |
Correct |
77 ms |
12412 KB |
Output is correct |
41 |
Correct |
159 ms |
17268 KB |
Output is correct |
42 |
Correct |
71 ms |
15568 KB |
Output is correct |
43 |
Correct |
42 ms |
10940 KB |
Output is correct |
44 |
Correct |
70 ms |
16080 KB |
Output is correct |
45 |
Correct |
80 ms |
15556 KB |
Output is correct |
46 |
Correct |
58 ms |
11228 KB |
Output is correct |
47 |
Correct |
71 ms |
13252 KB |
Output is correct |
48 |
Correct |
73 ms |
12656 KB |
Output is correct |
49 |
Correct |
163 ms |
18848 KB |
Output is correct |
50 |
Correct |
66 ms |
16584 KB |
Output is correct |
51 |
Correct |
59 ms |
11720 KB |
Output is correct |
52 |
Correct |
88 ms |
15300 KB |
Output is correct |
53 |
Correct |
72 ms |
16068 KB |
Output is correct |
54 |
Correct |
77 ms |
16064 KB |
Output is correct |
55 |
Correct |
119 ms |
16824 KB |
Output is correct |