제출 #529733

#제출 시각아이디문제언어결과실행 시간메모리
529733Alex_tz307Global Warming (CEOI18_glo)C++17
100 / 100
277 ms6644 KiB
#include <bits/stdc++.h> using namespace std; void maxSelf(int &x, int y) { if (x < y) { x = y; } } struct ST { int n; vector<int> t; ST(int N) : n(N) { int dim = 1; while (dim < n) { dim *= 2; } t.resize(dim * 2); } void reset() { for (int &it : t) { it = 0; } } void update(int x, int lx, int rx, int pos, int v) { if (lx == rx) { t[x] = v; return; } int mid = (lx + rx) / 2; if (pos <= mid) { update(x * 2, lx, mid, pos, v); } else { update(x * 2 + 1, mid + 1, rx, pos, v); } t[x] = max(t[x * 2], t[x * 2 + 1]); } void update(int pos, int v) { update(1, 0, n - 1, pos, v); } int query(int x, int lx, int rx, int st, int dr) { if (st <= lx && rx <= dr) { return t[x]; } int mid = (lx + rx) / 2, ans = 0; if (st <= mid) { maxSelf(ans, query(x * 2, lx, mid, st, dr)); } if (mid < dr) { maxSelf(ans, query(x * 2 + 1, mid + 1, rx, st, dr)); } return ans; } int query(int st, int dr) { if (dr < st) { return 0; } return query(1, 0, n - 1, st, dr); } }; void testCase() { int n, x; cin >> n >> x; vector<int> a(n); for (int &x : a) { cin >> x; } vector<int> v = a; sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); int m = v.size(); ST t(m); vector<int> dp(n); auto getIndex = [&](const int &val) -> int { return distance(v.begin(), lower_bound(v.begin(), v.end(), val)); }; for (int i = n - 1; i >= 0; --i) { int index = getIndex(a[i]); dp[i] = t.query(index + 1, m - 1) + 1; t.update(index, dp[i]); } t.reset(); int ans = 0; for (int i = 0; i < n; ++i) { maxSelf(ans, t.query(0, getIndex(a[i] + x) - 1) + dp[i]); a[i] = getIndex(a[i]); int best = t.query(0, a[i] - 1) + 1; t.update(a[i], best); } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int tests = 1; for (int tc = 0; tc < tests; ++tc) { testCase(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...