#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0); cin.tie(0)
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx,avx2")
//#pragma GCC target("popcnt")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MAX=2e5+10,P=1e9+7;
const ll INF=0x3f3f3f3f,oo=0x3f3f3f3f3f3f3f3f;
const ldb eps=1e-6;
const ldb PI=acos(-1.0);
const int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
template<typename T>
using vvector = vector<vector<T>>;
int main() {
speed;
int n,x;
cin>>n>>x;
vector<int> a(n+1);
vector<int> dp(n+1);
for (int i=1;i<=n;i++) {
cin>>a[i];
}
vector<int> curr;
for (int i=1;i<=n;i++) {
if (curr.empty() or curr.back()<a[i]) {
curr.push_back(a[i]);
dp[i]=curr.size();
} else {
auto it = lower_bound(all(curr),a[i]);
*it = a[i];
dp[i]=it-curr.begin()+1;
}
// cout<<dp[i]<<" ";
}
// cout<<"dp\n";
curr.clear();
vector<int> dp2(n+1);
for (int i=n;i>=1;i--) {
if (curr.empty() or curr.back()>a[i]-x) dp2[i]=curr.size()+1;
else {
int l=0,r=curr.size()-1;
while (l<r) {
int mid=l+r>>1;
if (curr[mid]<=a[i]-x) r=mid;
else l=mid+1;
}
dp2[i]=l+1;
}
if (curr.empty() or curr.back()>a[i]) curr.push_back(a[i]);
else {
int l=0,r=curr.size()-1;
while (l<r) {
int mid=l+r>>1;
if (curr[mid]<=a[i]) r=mid;
else l=mid+1;
}
curr[l]=a[i];
}
// cout<<dp2[i]<<" ";
}
// cout<<"dp2\n";
int ans=0;
for (int i=1;i<=n;i++) {
// cout<<dp[i]<<" "<<dp2[i]<<" dp dp2\n";
ans=max(ans,dp[i]+dp2[i]-1);
}
cout<<ans<<"\n";
return 0;
}
/*
10 10
2 34 545 56 3 3 65 2 7 6
*/
Compilation message
glo.cpp: In function 'int main()':
glo.cpp:55:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
55 | int mid=l+r>>1;
| ~^~
glo.cpp:65:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
65 | int mid=l+r>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
456 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
456 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
508 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
504 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
508 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
456 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
508 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
504 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
508 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
2808 KB |
Output is correct |
2 |
Correct |
37 ms |
4720 KB |
Output is correct |
3 |
Correct |
37 ms |
4688 KB |
Output is correct |
4 |
Correct |
38 ms |
4680 KB |
Output is correct |
5 |
Correct |
18 ms |
4304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
848 KB |
Output is correct |
2 |
Correct |
9 ms |
1536 KB |
Output is correct |
3 |
Correct |
10 ms |
1360 KB |
Output is correct |
4 |
Correct |
5 ms |
1360 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
5 ms |
1392 KB |
Output is correct |
7 |
Correct |
8 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
1616 KB |
Output is correct |
2 |
Correct |
16 ms |
1508 KB |
Output is correct |
3 |
Correct |
33 ms |
2640 KB |
Output is correct |
4 |
Correct |
16 ms |
3280 KB |
Output is correct |
5 |
Correct |
8 ms |
1884 KB |
Output is correct |
6 |
Correct |
13 ms |
3024 KB |
Output is correct |
7 |
Correct |
17 ms |
3096 KB |
Output is correct |
8 |
Correct |
13 ms |
1500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
456 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
508 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
504 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
508 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
36 ms |
2808 KB |
Output is correct |
28 |
Correct |
37 ms |
4720 KB |
Output is correct |
29 |
Correct |
37 ms |
4688 KB |
Output is correct |
30 |
Correct |
38 ms |
4680 KB |
Output is correct |
31 |
Correct |
18 ms |
4304 KB |
Output is correct |
32 |
Correct |
9 ms |
848 KB |
Output is correct |
33 |
Correct |
9 ms |
1536 KB |
Output is correct |
34 |
Correct |
10 ms |
1360 KB |
Output is correct |
35 |
Correct |
5 ms |
1360 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
5 ms |
1392 KB |
Output is correct |
38 |
Correct |
8 ms |
1364 KB |
Output is correct |
39 |
Correct |
16 ms |
1616 KB |
Output is correct |
40 |
Correct |
16 ms |
1508 KB |
Output is correct |
41 |
Correct |
33 ms |
2640 KB |
Output is correct |
42 |
Correct |
16 ms |
3280 KB |
Output is correct |
43 |
Correct |
8 ms |
1884 KB |
Output is correct |
44 |
Correct |
13 ms |
3024 KB |
Output is correct |
45 |
Correct |
17 ms |
3096 KB |
Output is correct |
46 |
Correct |
13 ms |
1500 KB |
Output is correct |
47 |
Correct |
19 ms |
2528 KB |
Output is correct |
48 |
Correct |
19 ms |
2612 KB |
Output is correct |
49 |
Correct |
41 ms |
4752 KB |
Output is correct |
50 |
Correct |
18 ms |
4492 KB |
Output is correct |
51 |
Correct |
17 ms |
3536 KB |
Output is correct |
52 |
Correct |
16 ms |
4560 KB |
Output is correct |
53 |
Correct |
16 ms |
4504 KB |
Output is correct |
54 |
Correct |
18 ms |
5072 KB |
Output is correct |
55 |
Correct |
31 ms |
4688 KB |
Output is correct |