이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
template <typename T>
class fenwick {
public:
vector<T> fenw;
int n;
fenwick(int _n) : n(_n) {
fenw.resize(n);
}
void modify(int x, T v) {
while (x < n) {
fenw[x] += v;
x |= (x + 1);
}
}
T get(int x) {
T v{};
while (x >= 0) {
v += fenw[x];
x = (x & (x + 1)) - 1;
}
return v;
}
};
struct node {
int a = 0; // don't forget to set default value
inline void operator += (node &other) {
a = max(a, other.a);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, x;
cin >> n >> x;
vector<int> a(n);
vector<int> cs;
for (int i = 0; i < n; i++) {
cin >> a[i];
cs.push_back(a[i]);
cs.push_back(a[i] + x);
}
sort(cs.begin(), cs.end());
cs.erase(unique(cs.begin(), cs.end()), cs.end());
int ans = 0;
vector<fenwick<node>> fenw(2, fenwick<node>(n * 2));
for (int i = 0; i < n; i++) {
int vx = lower_bound(cs.begin(), cs.end(), a[i]) - cs.begin();
int dp1 = fenw[0].get(vx - 1).a + 1;
ans = max(ans, dp1);
fenw[0].modify(vx, {dp1});
int vy = lower_bound(cs.begin(), cs.end(), a[i] + x) - cs.begin();
int dp2 = max(dp1, fenw[1].get(vy - 1).a + 1);
ans = max(ans, dp2);
fenw[1].modify(vy, {dp2});
fenw[1].modify(vx, {dp1});
}
cout << ans << '\n';
return 0;
}
# | 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... |