inline ll rd(){ ll x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
deque<int> Q; ll a[N]; int fi[N], fa[N], n, k;
inlinevoidwork(){ n = rd(), k = rd(); for (int i = 1; i <= n; ++i) a[i] = rd(); Q.clear(); for (int i = 1; i <= n; ++i) { while (!Q.empty() && Q.front() < i - k + 1) Q.pop_front(); while (!Q.empty() && a[Q.back()] >= a[i]) Q.pop_back(); Q.push_back(i); if (!Q.empty()) fi[i] = Q.front(); } Q.clear(); for (int i = 1; i <= n; ++i) { while (!Q.empty() && Q.front() < i - k + 1) Q.pop_front(); while (!Q.empty() && a[Q.back()] <= a[i]) Q.pop_back(); Q.push_back(i); if (!Q.empty()) fa[i] = Q.front(); } for (int i = k; i <= n; ++i) printf("%lld ", a[fi[i]]); puts(""); for (int i = k; i <= n; ++i) printf("%lld ", a[fa[i]]); }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
deque<int> mn, mx; int a[N]; int n, L, R;
inlinevoidwork(){ for (int i = 1; i <= n; ++i) a[i] = rd(); mn.clear(); mx.clear(); int l = 1, ans = 0; for (int r = 1; r <= n; ++r) { while (!mn.empty() && a[mn.back()] > a[r]) mn.pop_back(); while (!mx.empty() && a[mx.back()] < a[r]) mx.pop_back(); mx.push_back(r); mn.push_back(r); while (a[mx.front()] - a[mn.front()] > R) { if (mx.front() < mn.front()) { l = mx.front() + 1; mx.pop_front(); }elseif (mx.front() > mn.front()) { l = mn.front() + 1; mn.pop_front(); }else { l = mn.front() + 1; mn.pop_front(); mx.pop_front(); } } if (a[mx.front()] - a[mn.front()] >= L) ans = max(ans, r - l + 1); } printf("%d\n", ans); }
intmain(){ while (cin >> n >> L >> R) work(); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
deque<int> q; int f[N];
inlinevoidwork(){ int x = rd(), k = rd(), t = rd(); q.clear(); f[1] = 0; q.push_back(1); for (int i = 2; i <= x; ++i) { f[i] = 1000007; if (i % k == 0) f[i] = min(f[i/k] + 1, f[i]); while (!q.empty() && q.front() < i - t) q.pop_front(); if (!q.empty()) f[i] = min(f[q.front()] + 1, f[i]); while (!q.empty() && f[q.back()] >= f[i]) q.pop_back(); q.push_back(i); } printf("%d\n", f[x]); }
intmain(){ int t = rd(); while (t--) work(); return0; }
Post title:单调队列
Post author:Eva.Q
Create time:2022-02-04 22:26:44
Post link:https://qyy/2022/02/04/Algorithm/Monotonic deque/
Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.