#include <bits/stdc++.h>
using namespace std;
template<class T> struct Seg { // comb(ID,b) = b
const T ID = 0; T comb(T a, T b) { return max(a,b); }
int n; vector<T> seg;
void init(int _n) { n = _n; seg.assign(2*n,ID); }
void pull(int p) { seg[p] = comb(seg[2*p],seg[2*p+1]); }
void upd(int p, T val) { // set val at position p
seg[p += n] = val; for (p /= 2; p; p /= 2) pull(p); }
T query(int l, int r) { // min on interval [l, r]
T ra = ID, rb = ID;
for (l += n, r += n+1; l < r; l /= 2, r /= 2) {
if (l&1) ra = comb(ra,seg[l++]);
if (r&1) rb = comb(seg[--r],rb);
}
return comb(ra,rb);
}
};
Seg<int> st;
int main() {
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n,k; cin >> n >> k;
st.init(n+1);
vector<int> A(n); for(auto &a : A) cin >> a;
vector<int> X; for(int i : A) X.push_back(i);
sort(begin(X),end(X));
X.erase(unique(begin(X),end(X)),end(X));
auto index = [&] (int x) {
if (x > X.back()) return (int)X.size()-1;
return (int)(lower_bound(begin(X),end(X),x) - begin(X));
};
// find lds in reverse array to have lis starting at index i
vector<int> dp(n,0);
reverse(begin(A),end(A));
vector<int> lis;
for(int i = 0; i < n; i++) {
int pos = lower_bound(begin(lis),end(lis),A[i]) - begin(lis);
dp[i] = pos+1;
if (pos == lis.size()) lis.push_back(A[i]);
else lis[pos]=A[i];
}
reverse(begin(A),end(A));
reverse(begin(dp),end(dp));
int ans = 0;
for(int i = 0; i < n; i++) {
// check if we can get longer sequence
ans = max(ans,dp[i] + st.query(index(A[i])+1,index(A[i] + k)+1));
st.upd(index(A[i]) + 1, st.query(1,index(A[i]-1) + 1) + 1);
}
cout << ans << '\n';
}
Compilation message
glo.cpp: In function 'int main()':
glo.cpp:44:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | if (pos == lis.size()) lis.push_back(A[i]);
| ~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
156 ms |
6188 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
49 ms |
1864 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
78 ms |
3364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |