#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Fenwick{
vector<ll> arr;
ll n;
bool startFromLeft;
Fenwick(ll _n, bool _startFromLeft){
//0 to n-1 inclusive
startFromLeft = _startFromLeft;
n=_n;
arr.resize(n+1);
}
ll conv(ll ind){
ind+=1;
if (!startFromLeft){
ind=n+1-ind;
}
return ind;
}
void update(ll ind, ll val){
ind = conv(ind);
while (ind<=n){
arr[ind]=max(arr[ind],val);
ind+=ind&(-ind);
}
}
ll query(ll ind){
ind = conv(ind);
ll val = 0;
while (ind>0){
val=max(val,arr[ind]);
ind-=ind&(-ind);
}
return val;
}
};
vector<ll> vals;
vector<ll> mp;
vector<ll> lisl;
vector<ll> lisr;
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
ll n,d;
cin>>n>>d;
vals.resize(n);
mp.resize(n);
for (ll i = 0; i<n; i++){
cin>>vals[i];
mp[i]=vals[i];
}
sort(mp.begin(),mp.end());
mp.erase(unique(mp.begin(),mp.end()),mp.end());
for (ll i = 0; i<n; i++){
vals[i]=lower_bound(mp.begin(),mp.end(),vals[i])-mp.begin();
}
Fenwick *fenl = new Fenwick(n+1, true);
lisl.resize(n,0);
for (ll i = 0; i<n; i++){
//cout<<vals[i]<<'\n';
ll mxprev = fenl->query(vals[i]);
lisl[i]=mxprev+1;
fenl->update(vals[i]+1,lisl[i]);
}
Fenwick *fenr = new Fenwick(n+1, false);
lisr.resize(n,0);
ll mx = 0;
for (ll i = n-1; i>=0; i--){
//cout<<vals[i]<<'\n';
ll mxprev = fenr->query(vals[i]+1);
lisr[i]=mxprev+1;
fenr->update(vals[i],lisr[i]);
mx=max(mx,lisr[i]);
}
Fenwick *leftdat = new Fenwick(n+1, true);
for (ll i = 0; i<n; i++){
//use item i in rhs of lis
//how far above can we go?
//cout<<"find "<<mp[vals[i]]+d-1<<'\n';
ll mxup = lower_bound(mp.begin(),mp.end(),mp[vals[i]]+d)-mp.begin()-1;
//if (mxup==mp.size()){mxup--;}
ll mxval = leftdat->query(mxup);
//cout<<mxup<<' '<<mxval<<' '<<lisr[i]<<' '<<mxval+lisr[i]<<'\n';
mx=max(mx,mxval+lisr[i]);
leftdat->update(vals[i],lisl[i]);
//cout<<mx<<'\n';
}
cout<<mx<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
147 ms |
11256 KB |
Output is correct |
2 |
Correct |
145 ms |
11384 KB |
Output is correct |
3 |
Correct |
146 ms |
11256 KB |
Output is correct |
4 |
Correct |
146 ms |
11256 KB |
Output is correct |
5 |
Correct |
77 ms |
11256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
3072 KB |
Output is correct |
2 |
Correct |
34 ms |
3072 KB |
Output is correct |
3 |
Correct |
34 ms |
3072 KB |
Output is correct |
4 |
Correct |
21 ms |
3072 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
22 ms |
3072 KB |
Output is correct |
7 |
Correct |
31 ms |
3580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
5880 KB |
Output is correct |
2 |
Correct |
58 ms |
5880 KB |
Output is correct |
3 |
Correct |
127 ms |
11256 KB |
Output is correct |
4 |
Correct |
74 ms |
11256 KB |
Output is correct |
5 |
Correct |
40 ms |
5880 KB |
Output is correct |
6 |
Correct |
68 ms |
10744 KB |
Output is correct |
7 |
Correct |
74 ms |
10744 KB |
Output is correct |
8 |
Correct |
50 ms |
5880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
147 ms |
11256 KB |
Output is correct |
28 |
Correct |
145 ms |
11384 KB |
Output is correct |
29 |
Correct |
146 ms |
11256 KB |
Output is correct |
30 |
Correct |
146 ms |
11256 KB |
Output is correct |
31 |
Correct |
77 ms |
11256 KB |
Output is correct |
32 |
Correct |
35 ms |
3072 KB |
Output is correct |
33 |
Correct |
34 ms |
3072 KB |
Output is correct |
34 |
Correct |
34 ms |
3072 KB |
Output is correct |
35 |
Correct |
21 ms |
3072 KB |
Output is correct |
36 |
Correct |
4 ms |
384 KB |
Output is correct |
37 |
Correct |
22 ms |
3072 KB |
Output is correct |
38 |
Correct |
31 ms |
3580 KB |
Output is correct |
39 |
Correct |
58 ms |
5880 KB |
Output is correct |
40 |
Correct |
58 ms |
5880 KB |
Output is correct |
41 |
Correct |
127 ms |
11256 KB |
Output is correct |
42 |
Correct |
74 ms |
11256 KB |
Output is correct |
43 |
Correct |
40 ms |
5880 KB |
Output is correct |
44 |
Correct |
68 ms |
10744 KB |
Output is correct |
45 |
Correct |
74 ms |
10744 KB |
Output is correct |
46 |
Correct |
50 ms |
5880 KB |
Output is correct |
47 |
Correct |
73 ms |
6776 KB |
Output is correct |
48 |
Correct |
72 ms |
6776 KB |
Output is correct |
49 |
Correct |
151 ms |
13304 KB |
Output is correct |
50 |
Correct |
77 ms |
12428 KB |
Output is correct |
51 |
Correct |
70 ms |
9464 KB |
Output is correct |
52 |
Correct |
91 ms |
12664 KB |
Output is correct |
53 |
Correct |
75 ms |
12668 KB |
Output is correct |
54 |
Correct |
82 ms |
13304 KB |
Output is correct |
55 |
Correct |
124 ms |
13304 KB |
Output is correct |