#include <bits/stdc++.h>
using namespace std;
struct Fenwick{
vector<int> arr;
int n;
bool startFromLeft;
Fenwick(int _n, bool _startFromLeft){
//0 to n-1 inclusive
startFromLeft = _startFromLeft;
n=_n;
arr.resize(n+1);
}
int conv(int ind){
ind+=1;
if (!startFromLeft){
ind=n+1-ind;
}
return ind;
}
void update(int ind, int val){
ind = conv(ind);
while (ind<=n){
arr[ind]=max(arr[ind],val);
ind+=ind&(-ind);
}
}
int query(int ind){
ind = conv(ind);
int val = 0;
while (ind>0){
val=max(val,arr[ind]);
ind-=ind&(-ind);
}
return val;
}
};
vector<int> vals;
vector<int> mp;
vector<int> lisl;
vector<int> lisr;
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
int n,d;
cin>>n>>d;
vals.resize(n);
mp.resize(n);
for (int 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 (int 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 (int i = 0; i<n; i++){
//cout<<vals[i]<<'\n';
int 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);
int mx = 0;
for (int i = n-1; i>=0; i--){
//cout<<vals[i]<<'\n';
int 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 (int i = 0; i<n; i++){
//use item i in rhs of lis
//how far above can we go?
int mxup = upper_bound(mp.begin(),mp.end(),mp[i]+d)-mp.begin()-1;
int mxval = leftdat->query(mxup);
//cout<<mxup<<' '<<mxval<<' '<<lisr[i]<<'\n';
mx=max(mx,mxval+lisr[i]);
leftdat->update(vals[i]+1,lisl[i]);
//cout<<mx<<'\n';
}
cout<<mx<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
121 ms |
6008 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
30 ms |
1656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
3064 KB |
Output is correct |
2 |
Correct |
55 ms |
4088 KB |
Output is correct |
3 |
Correct |
120 ms |
7732 KB |
Output is correct |
4 |
Correct |
76 ms |
7144 KB |
Output is correct |
5 |
Correct |
39 ms |
3704 KB |
Output is correct |
6 |
Incorrect |
66 ms |
6776 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |