Submission #515736

#TimeUsernameProblemLanguageResultExecution timeMemory
515736MilosMilutinovicGlobal Warming (CEOI18_glo)C++14
100 / 100
149 ms9436 KiB
#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 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...