This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
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 = (1<<20); step>=1; step/=2){
if(pos+step<v.size() && v[pos+step]<=e){
pos+=step;
}
}
return pos;
}
vector<int> vals;
int lis(vector<int>& v){
MaxTree tr(vals.size());
for(int i= 0; i<N; i++){
int best_under = tr.get(0, v[i]-1);
tr.set(v[i], best_under+1);
}
return tr.get(0, vals.size()-1);
}
signed main(){
cin>>N>>X;
vector<int> t(N);
vals.push_back(0);
for(int i = 0; i<N; i++){
cin>>t[i];
vals.push_back(t[i]);
vals.push_back(t[i]+X);
}
sort(vals.begin(), vals.end());
for(int i = 0; i<N; i++){
t[i] = find_pos(t[i], vals);
}
int res= 0;
for(int l = 0; l<N; l++){
vector<int> nv = t;
for(int i =l; i<N; i++){
nv[i] = find_pos(vals[t[i]]+X, vals);
res = max(res, lis(nv));
}
}
cout<<res<<endl;
}
Compilation message (stderr)
glo.cpp: In function 'int find_pos(int, std::vector<int>&)':
glo.cpp:48:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | if(pos+step<v.size() && v[pos+step]<=e){
| ~~~~~~~~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |