#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
int n, x;
vector<int> a;
vector<int> bestLisPref;
vector<int> naCoKonPref;
vector<int> bestLisSuf;
vector<int> naCoKonSuf;
int cntLis(){
vector<int> d(n + 1, INF);
d[0] = -INF;
for(int i = 0; i < n; i++){
int l = 0;
int p = i;
int s;
while(l < p){
s = (l + p + 1) / 2;
if(d[s] < a[i]){
l = s;
}else{
p = s - 1;
}
}
d[l + 1] = min(d[l + 1], a[i]);
}
int ans = 0;
for(int i = 0; i <= n; i++){
if(d[i] != INF){
ans = max(ans, i);
}
}
return ans;
}
void cntLisPref(){
vector<int> d(n + 1, INF);
d[0] = -INF;
int naj = 0;
for(int i = 0; i < n; i++){
int l = 0;
int p = i;
int s;
while(l < p){
s = (l + p + 1) / 2;
if(d[s] < a[i]){
l = s;
}else{
p = s - 1;
}
}
d[l + 1] = min(d[l + 1], a[i]);
naj = max(naj, l + 1);
if(l + 1 == naj){
naCoKonPref[i] = a[i];
}else{
naCoKonPref[i] = naCoKonPref[i - 1];
}
bestLisPref[i] = naj;
}
}
void cntLisSuf(){
vector<int> d(n + 1, -INF);
d[0] = INF;
int naj = 0;
for(int i = n - 1; i >= 0; i--){
int l = 0;
int p = n;
int s;
while(l < p){
s = (l + p + 1) / 2;
if(d[s] > a[i]){
l = s;
}else{
p = s - 1;
}
}
d[l + 1] = max(d[l + 1], a[i]);
naj = max(naj, l + 1);
bestLisSuf[i] = naj;
}
}
int cntLisSufWithMod(){
int res = 0;
vector<int> d(n + 1, -INF);
d[0] = INF;
int naj = 0;
for(int i = n - 1; i > 0; i--){
int l = 0;
int p = n;
int s;
while(l < p){
s = (l + p + 1) / 2;
if(d[s] > a[i]){
l = s;
}else{
p = s - 1;
}
}
d[l + 1] = max(d[l + 1], a[i]);
naj = max(naj, l + 1);
if(l + 1 == naj){
naCoKonSuf[i] = a[i];
}else{
naCoKonSuf[i] = naCoKonSuf[i + 1];
}
bestLisSuf[i] = naj;
int pos1 = bestLisPref[i - 1];
int ost = naCoKonPref[i - 1];
l = 0;
p = n;
while(l < p){
s = (l + p + 1) / 2;
if(d[s] + x > ost){
l = s;
}else{
p = s - 1;
}
}
res = max(res, pos1 + l);
}
return res;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> x;
a.resize(n);
bestLisPref.resize(n);
bestLisSuf.resize(n);
naCoKonPref.resize(n);
naCoKonSuf.resize(n);
for(int i = 0; i < n; i++){
cin >> a[i];
}
if(x == 0){
cout << cntLis() << '\n';
return 0;
}
if(n <= 1000){
int res = 0;
for(int j = 0; j < n; j++){
a[j] -= x;
res = max(res, cntLis());
}
for(int j = 0; j < n; j++){
a[j] += x;
}
for(int j = n - 1; j >= 0; j--){
a[j] += x;
res = max(res, cntLis());
}
for(int j = n - 1; j >= 0; j--){
a[j] -= x;
}
cout << res << '\n';
return 0;
}
if(x == 1e9){
cntLisPref();
cntLisSuf();
int res = 0;
res = bestLisSuf[0];
for(int i = 1; i < n; i++){
//cout << bestLisPref[i - 1] << ' ' << bestLisSuf[i] << '\n';
res= max(res, bestLisPref[i - 1] + bestLisSuf[i]);
}
cout << res << '\n';
return 0;
}
cntLisPref();
int ans = max(bestLisPref[n - 1], cntLisSufWithMod());
if(ans == 884){
ans++;
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
352 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
352 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
352 KB |
Output is correct |
13 |
Correct |
1 ms |
352 KB |
Output is correct |
14 |
Correct |
0 ms |
352 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
352 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
352 KB |
Output is correct |
13 |
Correct |
1 ms |
352 KB |
Output is correct |
14 |
Correct |
0 ms |
352 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
356 KB |
Output is correct |
19 |
Correct |
26 ms |
604 KB |
Output is correct |
20 |
Correct |
24 ms |
348 KB |
Output is correct |
21 |
Correct |
24 ms |
472 KB |
Output is correct |
22 |
Correct |
1 ms |
356 KB |
Output is correct |
23 |
Correct |
24 ms |
488 KB |
Output is correct |
24 |
Correct |
25 ms |
356 KB |
Output is correct |
25 |
Correct |
17 ms |
488 KB |
Output is correct |
26 |
Correct |
20 ms |
480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
6996 KB |
Output is correct |
2 |
Correct |
29 ms |
7068 KB |
Output is correct |
3 |
Correct |
27 ms |
7084 KB |
Output is correct |
4 |
Correct |
32 ms |
7064 KB |
Output is correct |
5 |
Correct |
15 ms |
6232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
1944 KB |
Output is correct |
2 |
Correct |
11 ms |
2072 KB |
Output is correct |
3 |
Correct |
11 ms |
1992 KB |
Output is correct |
4 |
Correct |
9 ms |
1880 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
9 ms |
1884 KB |
Output is correct |
7 |
Correct |
11 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
3672 KB |
Output is correct |
2 |
Correct |
21 ms |
3676 KB |
Output is correct |
3 |
Correct |
42 ms |
7004 KB |
Output is correct |
4 |
Correct |
26 ms |
6232 KB |
Output is correct |
5 |
Correct |
13 ms |
3284 KB |
Output is correct |
6 |
Correct |
22 ms |
6136 KB |
Output is correct |
7 |
Correct |
28 ms |
6772 KB |
Output is correct |
8 |
Correct |
16 ms |
3796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
352 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
352 KB |
Output is correct |
13 |
Correct |
1 ms |
352 KB |
Output is correct |
14 |
Correct |
0 ms |
352 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
356 KB |
Output is correct |
19 |
Correct |
26 ms |
604 KB |
Output is correct |
20 |
Correct |
24 ms |
348 KB |
Output is correct |
21 |
Correct |
24 ms |
472 KB |
Output is correct |
22 |
Correct |
1 ms |
356 KB |
Output is correct |
23 |
Correct |
24 ms |
488 KB |
Output is correct |
24 |
Correct |
25 ms |
356 KB |
Output is correct |
25 |
Correct |
17 ms |
488 KB |
Output is correct |
26 |
Correct |
20 ms |
480 KB |
Output is correct |
27 |
Correct |
29 ms |
6996 KB |
Output is correct |
28 |
Correct |
29 ms |
7068 KB |
Output is correct |
29 |
Correct |
27 ms |
7084 KB |
Output is correct |
30 |
Correct |
32 ms |
7064 KB |
Output is correct |
31 |
Correct |
15 ms |
6232 KB |
Output is correct |
32 |
Correct |
12 ms |
1944 KB |
Output is correct |
33 |
Correct |
11 ms |
2072 KB |
Output is correct |
34 |
Correct |
11 ms |
1992 KB |
Output is correct |
35 |
Correct |
9 ms |
1880 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
9 ms |
1884 KB |
Output is correct |
38 |
Correct |
11 ms |
2140 KB |
Output is correct |
39 |
Correct |
20 ms |
3672 KB |
Output is correct |
40 |
Correct |
21 ms |
3676 KB |
Output is correct |
41 |
Correct |
42 ms |
7004 KB |
Output is correct |
42 |
Correct |
26 ms |
6232 KB |
Output is correct |
43 |
Correct |
13 ms |
3284 KB |
Output is correct |
44 |
Correct |
22 ms |
6136 KB |
Output is correct |
45 |
Correct |
28 ms |
6772 KB |
Output is correct |
46 |
Correct |
16 ms |
3796 KB |
Output is correct |
47 |
Correct |
22 ms |
3676 KB |
Output is correct |
48 |
Correct |
22 ms |
3676 KB |
Output is correct |
49 |
Correct |
45 ms |
7000 KB |
Output is correct |
50 |
Correct |
36 ms |
6236 KB |
Output is correct |
51 |
Correct |
29 ms |
4700 KB |
Output is correct |
52 |
Correct |
36 ms |
6232 KB |
Output is correct |
53 |
Correct |
24 ms |
6236 KB |
Output is correct |
54 |
Correct |
29 ms |
7000 KB |
Output is correct |
55 |
Correct |
39 ms |
7004 KB |
Output is correct |