#include<bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
int N, X;
struct MaxTree{
int len = 1;
vector<int> tr;
MaxTree(int n){
while(len<n){
len*=2;
}
tr.resize(2*len);
}
void upd(int u){
for(u/=2; u>=1; u/=2){
tr[u] = max(tr[u*2], tr[u*2+1]);
}
}
int get(int l, int r){
l += len;
r+=len+1;
int res = 0;
for(; l<r; l/=2, r/=2){
if(l%2 ==1){
res = max(res, tr[l++]);
}
if(r%2 == 1){
res = max(res, tr[--r]);
}
}
return res;
}
void set(int pos, int val){
pos+=len;
tr[pos] = max(tr[pos], val);
upd(pos);
}
};
int find_pos(int e, vector<int>& v){
int pos =0;
for(int step = (1LL<<20); step>=1; step/=2){
if(pos+step<v.size() && v[pos+step]<=e){
pos+=step;
}
}
return pos;
}
vector<int> vals;
int lis(vector<pii>& v){
MaxTree tr(vals.size());
for(int i= 0; i<N; i++){
int best_above = tr.get(0, v[i].second-1);
tr.set(v[i].second, best_above+1);
int best_under = tr.get(0, v[i].first-1);
tr.set(v[i].first, best_under+1);
}
return tr.get(0, vals.size()-1);
}
signed main(){
cin>>N>>X;
vector<pii> t(N);
vals.push_back(0);
for(int i = 0; i<N; i++){
cin>>t[i].first;
vals.push_back(t[i].first);
vals.push_back(t[i].first+X);
}
sort(vals.begin(), vals.end());
for(int i = 0; i<N; i++){
t[i] ={find_pos(t[i].first, vals), find_pos(t[i].first+X, vals)};
}
cout<<lis(t)<<endl;
}
Compilation message
glo.cpp: In function 'int find_pos(int, std::vector<int>&)':
glo.cpp:50:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | if(pos+step<v.size() && v[pos+step]<=e){
| ~~~~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
140 ms |
7600 KB |
Output is correct |
2 |
Correct |
141 ms |
7792 KB |
Output is correct |
3 |
Correct |
164 ms |
7616 KB |
Output is correct |
4 |
Correct |
136 ms |
7784 KB |
Output is correct |
5 |
Correct |
83 ms |
7792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
2256 KB |
Output is correct |
2 |
Correct |
29 ms |
2260 KB |
Output is correct |
3 |
Correct |
29 ms |
2252 KB |
Output is correct |
4 |
Incorrect |
21 ms |
2260 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
4040 KB |
Output is correct |
2 |
Correct |
64 ms |
4048 KB |
Output is correct |
3 |
Correct |
139 ms |
7876 KB |
Output is correct |
4 |
Correct |
97 ms |
7836 KB |
Output is correct |
5 |
Correct |
47 ms |
4092 KB |
Output is correct |
6 |
Correct |
98 ms |
7620 KB |
Output is correct |
7 |
Correct |
100 ms |
7628 KB |
Output is correct |
8 |
Correct |
58 ms |
4040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |