#include<bits/stdc++.h>
#define MAXN 200007
using namespace std;
const int inf=1e9;
struct ST{
struct node{
int l,r,val;
};
node tree[30*MAXN];
int num;
void init(){
num=1;
tree[num]={0,0,0};
}
void addnode(){
num++;
tree[num]={0,0,0};
}
void check(int v){
if(tree[v].l==0){
addnode(); tree[v].l=num;
}
if(tree[v].r==0){
addnode(); tree[v].r=num;
}
}
void update(int v,long long l,long long r,int pos,int val){
if(l==r){
tree[v].val=max(tree[v].val,val);
}else{
long long tt=(l+r)/2;
check(v);
if(pos<=tt)update(tree[v].l,l,tt,pos,val);
else update(tree[v].r,tt+1,r,pos,val);
tree[v].val=max(tree[tree[v].l].val,tree[tree[v].r].val);
}
}
int getmax(int v,long long l,long long r,long long ll,long long rr){
if(ll>rr or v==0)return 0;
if(l==ll and r==rr){
return tree[v].val;
}else{
long long tt=(l+r)/2;
return max( getmax(tree[v].l,l,tt,ll,min(tt,rr)) , getmax(tree[v].r,tt+1,r,max(tt+1,ll),rr) );
}
}
}seg[3];
int n,a[MAXN],ans,x;
int dp[MAXN][3];
int solve(int d){
seg[0].init();
seg[1].init();
seg[2].init();
int res=0;
for(int i=1;i<=n;i++){
dp[i][0]=seg[0].getmax(1,0,2*inf,0,a[i]-1)+1;
dp[i][1]=max(seg[1].getmax(1,0,2*inf,0,a[i]-1)+1 , seg[0].getmax(1,0,2*inf,0,a[i]+d-1)+1);
dp[i][2]=max(seg[2].getmax(1,0,2*inf,0,a[i]-1)+1 , seg[1].getmax(1,0,2*inf,0,a[i]-d-1)+1);
seg[0].update(1,0,2*inf,a[i],dp[i][0]);
seg[1].update(1,0,2*inf,a[i],dp[i][1]);
seg[2].update(1,0,2*inf,a[i],dp[i][2]);
res=max(res,max(dp[i][0],max(dp[i][1],dp[i][2])));
}
return res;
}
int sol[2000007],pos;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>x;
for(int i=1;i<=n;i++){
cin>>a[i];
}
if(n<=1000){
vector<int> w={0};
for(int i=1;i<=n;i++){
for(int f=i+1;f<=n;f++){
if(a[i]-a[f]+1>=-x and a[i]-a[f]+1<=x){
w.push_back(a[i]-a[f]+1);
}
if(a[i]-a[f]-1>=-x and a[i]-a[f]-1<=x){
w.push_back(a[i]-a[f]-1);
}
}
}
sort(w.begin(),w.end());
for(int i=0;i<w.size();i+=max(1,int(w.size())/1000)){
sol[i]=solve(w[i]);
if(sol[i]>ans){
ans=sol[i]; pos=i;
}
}
for(int i=max(0,pos-500);i<=min(int(w.size())-1,pos+500);i++){
ans=max(ans,solve(w[i]));
}
cout<<ans<<"\n";
return 0;
}
long long l=-x,r=x,lt,rt;
while(l+20<r){
lt=l+(r-l+1)/3;
rt=r-(r-l+1)/3;
int ls=solve(lt);
int rs=solve(rt);
if(ls>rs)l=lt;
else if(rs>ls)r=rt;
else{
break;
}
}
for(int d=l;d<=r;d++){
ans=max(ans,solve(d));
}
cout<<ans<<"\n";
return 0;
}
Compilation message
glo.cpp: In function 'int main()':
glo.cpp:115:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | for(int i=0;i<w.size();i+=max(1,int(w.size())/1000)){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 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 |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 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 |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
93 ms |
344 KB |
Output is correct |
13 |
Correct |
21 ms |
344 KB |
Output is correct |
14 |
Correct |
123 ms |
504 KB |
Output is correct |
15 |
Correct |
79 ms |
348 KB |
Output is correct |
16 |
Correct |
87 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
6 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 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 |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
93 ms |
344 KB |
Output is correct |
13 |
Correct |
21 ms |
344 KB |
Output is correct |
14 |
Correct |
123 ms |
504 KB |
Output is correct |
15 |
Correct |
79 ms |
348 KB |
Output is correct |
16 |
Correct |
87 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
6 ms |
348 KB |
Output is correct |
19 |
Correct |
1788 ms |
2316 KB |
Output is correct |
20 |
Correct |
27 ms |
1880 KB |
Output is correct |
21 |
Correct |
4 ms |
1884 KB |
Output is correct |
22 |
Correct |
4 ms |
1884 KB |
Output is correct |
23 |
Correct |
1570 ms |
3156 KB |
Output is correct |
24 |
Correct |
1539 ms |
3256 KB |
Output is correct |
25 |
Correct |
1481 ms |
5540 KB |
Output is correct |
26 |
Correct |
1755 ms |
1996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
462 ms |
179656 KB |
Output is correct |
2 |
Correct |
463 ms |
179584 KB |
Output is correct |
3 |
Correct |
494 ms |
179556 KB |
Output is correct |
4 |
Correct |
497 ms |
179540 KB |
Output is correct |
5 |
Correct |
269 ms |
98820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
920 ms |
52456 KB |
Output is correct |
2 |
Correct |
994 ms |
52452 KB |
Output is correct |
3 |
Correct |
920 ms |
52312 KB |
Output is correct |
4 |
Correct |
589 ms |
28500 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
6 |
Correct |
624 ms |
5044 KB |
Output is correct |
7 |
Correct |
741 ms |
36688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2053 ms |
97108 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 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 |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
93 ms |
344 KB |
Output is correct |
13 |
Correct |
21 ms |
344 KB |
Output is correct |
14 |
Correct |
123 ms |
504 KB |
Output is correct |
15 |
Correct |
79 ms |
348 KB |
Output is correct |
16 |
Correct |
87 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
6 ms |
348 KB |
Output is correct |
19 |
Correct |
1788 ms |
2316 KB |
Output is correct |
20 |
Correct |
27 ms |
1880 KB |
Output is correct |
21 |
Correct |
4 ms |
1884 KB |
Output is correct |
22 |
Correct |
4 ms |
1884 KB |
Output is correct |
23 |
Correct |
1570 ms |
3156 KB |
Output is correct |
24 |
Correct |
1539 ms |
3256 KB |
Output is correct |
25 |
Correct |
1481 ms |
5540 KB |
Output is correct |
26 |
Correct |
1755 ms |
1996 KB |
Output is correct |
27 |
Correct |
462 ms |
179656 KB |
Output is correct |
28 |
Correct |
463 ms |
179584 KB |
Output is correct |
29 |
Correct |
494 ms |
179556 KB |
Output is correct |
30 |
Correct |
497 ms |
179540 KB |
Output is correct |
31 |
Correct |
269 ms |
98820 KB |
Output is correct |
32 |
Correct |
920 ms |
52456 KB |
Output is correct |
33 |
Correct |
994 ms |
52452 KB |
Output is correct |
34 |
Correct |
920 ms |
52312 KB |
Output is correct |
35 |
Correct |
589 ms |
28500 KB |
Output is correct |
36 |
Correct |
4 ms |
348 KB |
Output is correct |
37 |
Correct |
624 ms |
5044 KB |
Output is correct |
38 |
Correct |
741 ms |
36688 KB |
Output is correct |
39 |
Execution timed out |
2053 ms |
97108 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |