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>
#define int long long
using namespace std;
const int maxn = 2e5 + 5;
int v[maxn], bit[2*maxn];
vector<int> ids;
int n;
void upd(int x, int val){
for(; x < 2*maxn; x+=x&-x) {bit[x] = max(bit[x], val);}
}
int mx(int x){
int rt = 0;
for(; x>0; x-=x&-x) rt = max(rt, bit[x]);
return rt;
}
int bbin(int x){
int l = 0, r = ids.size()-1, rt;
while(l<=r){
int mid = (l+r)>>1;
if(ids[mid] > x) r = mid-1;
else if(ids[mid] < x) l = mid+1;
else {rt = mid; break;}
}
return rt+1;
}
int32_t main(){
int x; cin >> n >> x;
x = -abs(x);
for(int i = 0; i < n; i++){
cin >> v[i];
ids.push_back(v[i]);
ids.push_back(v[i]+x);
}
sort(ids.begin(), ids.end());
ids.erase(unique(ids.begin(), ids.end()), ids.end());
// todas as coordenadas que importam estão comprimidas. Para acessar
// a coordenada comprimida use "bbin(val)" e ela retornará o id relativo (comprimido)
// 0-indexado + 1
int ans = 1;
for(int i = 0; i < n; i++){
int id1 = bbin(v[i]);
int id2 = bbin(v[i]+x);
int sz_lis1 = mx(id1-1)+1;
int sz_lis2 = mx(id2-1)+1;
ans = max({ans, sz_lis1, sz_lis2});
upd(id1, sz_lis1);
upd(id2, sz_lis2);
}
cout << ans << '\n';
}
Compilation message (stderr)
glo.cpp: In function 'long long int bbin(long long int)':
glo.cpp:27:12: warning: 'rt' may be used uninitialized in this function [-Wmaybe-uninitialized]
27 | return rt+1;
| ^
# | 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... |