#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define ss second
#define ff first
#define ll long long
#define pb push_back
#define mp make_pair
const int N = 2*1e5+11;
int n, k, a[N], f1[N], f2[N], res;
pair <int, int> b[N];
int dp[3][N], c[N], d[N];
int upit1 (int x) {
if (x > n) x = n;
int ret = 0;
for (; x > 0; x-=x&-x) {
ret = max(ret, f1[x]);
}
return ret;
}
int upit2 (int x) {
if (x > n) x = n;
int ret = 0;
for (; x > 0; x-=x&-x) {
ret = max(ret, f2[x]);
}
return ret;
}
void ubaci1 (int x, int kol) {
for (; x <= n; x+=x&-x) {
f1[x] = max(f1[x], kol);
}
}
void ubaci2 (int x, int kol) {
for (; x <= n; x+=x&-x) {
f2[x] = max(f2[x], kol);
}
}
int main() {
cin >> n >> k;
for (int i=0;i<n;i++) {
scanf("%d", &a[i]);
b[i].ff = a[i];
b[i].ss = i;
}
sort (b, b+n);
int br = 1;
c[b[0].ss] = 1;
for (int i=1;i<n;i++) {
if (b[i].ff != b[i-1].ff) br++;
c[b[i].ss] = br;
}
int j = 0;
for (int i=0;i<n;i++) {
if (b[i].ff-k >= b[j].ff) {
while (b[i].ff-k >= b[j].ff && j < n) {
if (i > 0) d[b[j].ss] = c[b[i-1].ss];
else d[b[j].ss] = -1;
j++;
}
}
}
/* for (int i=0;i<n;i++) cout << c[i] << " ";
cout << endl;
for (int i=0;i<n;i++) cout << d[i] << " ";
cout << endl;
*/ for (int i=0;i<n;i++) {
int x = c[i];
if (d[i] == 0) d[i] = n;
dp[1][i] = max(upit2(x-1), upit1(d[i]))+1;
ubaci2(x, dp[1][i]);
dp[0][i] = upit1(x-1)+1;
ubaci1(x, dp[0][i]);
res = max(res, dp[0][i]);
res = max(res, dp[1][i]);
}
cout << res;
return 0;
}
Compilation message
glo.cpp: In function 'int main()':
glo.cpp:51:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i]);
~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
372 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
372 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
404 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
372 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
404 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
504 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
7332 KB |
Output is correct |
2 |
Correct |
89 ms |
9308 KB |
Output is correct |
3 |
Correct |
89 ms |
9336 KB |
Output is correct |
4 |
Correct |
90 ms |
9248 KB |
Output is correct |
5 |
Correct |
68 ms |
7800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
2168 KB |
Output is correct |
2 |
Correct |
22 ms |
2040 KB |
Output is correct |
3 |
Correct |
22 ms |
2116 KB |
Output is correct |
4 |
Correct |
17 ms |
1912 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
13 ms |
1912 KB |
Output is correct |
7 |
Correct |
19 ms |
2168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
3804 KB |
Output is correct |
2 |
Correct |
43 ms |
3832 KB |
Output is correct |
3 |
Correct |
91 ms |
7456 KB |
Output is correct |
4 |
Correct |
68 ms |
6648 KB |
Output is correct |
5 |
Correct |
26 ms |
3832 KB |
Output is correct |
6 |
Correct |
51 ms |
7160 KB |
Output is correct |
7 |
Correct |
52 ms |
7064 KB |
Output is correct |
8 |
Correct |
37 ms |
3832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
372 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
404 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
504 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
88 ms |
7332 KB |
Output is correct |
28 |
Correct |
89 ms |
9308 KB |
Output is correct |
29 |
Correct |
89 ms |
9336 KB |
Output is correct |
30 |
Correct |
90 ms |
9248 KB |
Output is correct |
31 |
Correct |
68 ms |
7800 KB |
Output is correct |
32 |
Correct |
22 ms |
2168 KB |
Output is correct |
33 |
Correct |
22 ms |
2040 KB |
Output is correct |
34 |
Correct |
22 ms |
2116 KB |
Output is correct |
35 |
Correct |
17 ms |
1912 KB |
Output is correct |
36 |
Correct |
2 ms |
376 KB |
Output is correct |
37 |
Correct |
13 ms |
1912 KB |
Output is correct |
38 |
Correct |
19 ms |
2168 KB |
Output is correct |
39 |
Correct |
44 ms |
3804 KB |
Output is correct |
40 |
Correct |
43 ms |
3832 KB |
Output is correct |
41 |
Correct |
91 ms |
7456 KB |
Output is correct |
42 |
Correct |
68 ms |
6648 KB |
Output is correct |
43 |
Correct |
26 ms |
3832 KB |
Output is correct |
44 |
Correct |
51 ms |
7160 KB |
Output is correct |
45 |
Correct |
52 ms |
7064 KB |
Output is correct |
46 |
Correct |
37 ms |
3832 KB |
Output is correct |
47 |
Correct |
45 ms |
4844 KB |
Output is correct |
48 |
Correct |
45 ms |
4856 KB |
Output is correct |
49 |
Correct |
93 ms |
9296 KB |
Output is correct |
50 |
Correct |
69 ms |
7928 KB |
Output is correct |
51 |
Correct |
49 ms |
6264 KB |
Output is correct |
52 |
Correct |
57 ms |
8696 KB |
Output is correct |
53 |
Correct |
54 ms |
8572 KB |
Output is correct |
54 |
Correct |
57 ms |
9336 KB |
Output is correct |
55 |
Correct |
79 ms |
9336 KB |
Output is correct |