Submission #977218

#TimeUsernameProblemLanguageResultExecution timeMemory
977218LOLOLOGlobal Warming (CEOI18_glo)C++17
100 / 100
385 ms8824 KiB
#include <bits/stdc++.h> typedef long long ll; using namespace std; #define f first #define s second #define pb push_back #define ep emplace #define eb emplace_back #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin()) #define mem(f,x) memset(f , x , sizeof(f)) #define sz(x) (int)(x).size() #define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b)) #define mxx *max_element #define mnn *min_element #define cntbit(x) __builtin_popcountll(x) #define len(x) (int)(x.length()) const int N = 2e5 + 100; struct st{ vector <int> seg; void ass(int n) { seg.assign(n * 4 + 100, 0); } void upd(int id, int l, int r, int p, int v) { if (l > p || r < p) return; seg[id] = max(seg[id], v); if (l == r) return; int m = (l + r) / 2; upd(id * 2, l, m, p, v); upd(id * 2 + 1, m + 1, r, p, v); seg[id] = max(seg[id * 2], seg[id * 2 + 1]); } int get(int id, int l, int r, int u, int v) { if (l > v || r < u || u > v) return 0; int m = (l + r) / 2; if (l >= u && r <= v) return seg[id]; return max(get(id * 2, l, m, u, v), get(id * 2 + 1, m + 1, r, u, v)); } }; int a[N]; vector <int> val; int find(int x) { int p = lower_bound(all(val), x) - val.begin() + 1; return p; } int pr[N], suff[N]; ll solve() { int n, x; cin >> n >> x; for (int i = 1; i <= n; i++) { cin >> a[i]; val.pb(a[i]); } uniquev(val); st seg; seg.ass(n); for (int i = 1; i <= n; i++) { int p = find(a[i]); pr[i] = seg.get(1, 1, n, 1, p - 1) + 1; seg.upd(1, 1, n, p, pr[i]); } seg.ass(n); for (int i = n; i >= 1; i--) { int p = find(a[i]); suff[i] = seg.get(1, 1, n, p + 1, n) + 1; seg.upd(1, 1, n, p, suff[i]); } int ans = 0; for (int i = 1; i <= n; i++) ans = max(ans, pr[i]); seg.ass(n); for (int i = 1; i < n; i++) { int p = find(a[i]); seg.upd(1, 1, n, p, pr[i]); int nxt = find(a[i + 1] + x - 1); ans = max(ans, seg.get(1, 1, n, 1, nxt - 1) + suff[i + 1]); } seg.ass(n); for (int i = n; i > 1; i--) { int p = find(a[i]); seg.upd(1, 1, n, p, suff[i]); int nxt = find(a[i - 1] - x + 1); ans = max(ans, seg.get(1, 1, n, nxt, n) + pr[i - 1]); } return ans; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; //cin >> t; while (t--) { //solve(); cout << solve() << '\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...