P2698 [USACO12MAR]花盆Flowerpot 单调队列
警示
用数组写双端队列的话,记得le = 1, ri = 0;
le<=ri表示队列非空
题意
求一个最小的区间长度,使得区间中的最大值和最小值的差>=D.
思路
一开始二分加线段树强行做,多了一个log。用ST表可能会优秀。做到nlogn。
但是如果用单调队列的话,除去排序,就可以做到O(n)
具体来说,对于一个L,合法的最小的右区间若为R,那么L+1的最小合法右区间一定>=R.
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> using namespace std; #define lson (l, mid, rt << 1) #define rson (mid + 1, r, rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef pair<int, pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl '\n' #define boost \ ios::sync_with_stdio(false); \ cin.tie(0) #define rep(a, b, c) for (int a = (b); a <= (c); ++a) #define max3(a, b, c) max(max(a, b), c); #define min3(a, b, c) min(min(a, b), c); const ll oo = 1ll << 17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9 + 7; const double esp = 1e-8; const double PI = acos(-1.0); const double PHI = 0.61803399; //黄金分割点 const double tPHI = 0.38196601; template <typename T> inline T read(T &x) { x = 0; int f = 0; char ch = getchar(); while (ch < '0' || ch > '9') f |= (ch == '-'), ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x = f ? -x : x; } struct FastIO { static const int S = 4e6; int wpos; char wbuf[S]; FastIO() : wpos(0) { } inline int xchar() { static char buf[S]; static int len = 0, pos = 0; if (pos == len) pos = 0, len = fread(buf, 1, S, stdin); if (pos == len) exit(0); return buf[pos++]; } inline int xuint() { int c = xchar(), x = 0; while (c <= 32) c = xchar(); for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0'; return x; } inline int xint() { int s = 1, c = xchar(), x = 0; while (c <= 32) c = xchar(); if (c == '-') s = -1, c = xchar(); for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0'; return x * s; } inline void xstring(char *s) { int c = xchar(); while (c <= 32) c = xchar(); for (; c > 32; c = xchar()) *s++ = c; *s = 0; } inline void wchar(int x) { if (wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0; wbuf[wpos++] = x; } inline void wint(int x) { if (x < 0) wchar('-'), x = -x; char s[24]; int n = 0; while (x || !n) s[n++] = '0' + x % 10, x /= 10; while (n--) wchar(s[n]); wchar('\n'); } inline void wstring(const char *s) { while (*s) wchar(*s++); } ~FastIO() { if (wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0; } } io; inline void cmax(int &x, int y) { if (x < y) x = y; } inline void cmax(ll &x, ll y) { if (x < y) x = y; } inline void cmin(int &x, int y) { if (x > y) x = y; } inline void cmin(ll &x, ll y) { if (x > y) x = y; } /*-----------------------showtime----------------------*/ const int maxn = 100009; struct node { int x, y; } a[maxn]; bool cmp(node a, node b) { return a.x < b.x; } int dq1[maxn * 2], dq2[maxn * 2]; int main() { int n, D; scanf("%d%d", &n, &D); rep(i, 1, n) { scanf("%d%d", &a[i].x, &a[i].y); } sort(a + 1, a + 1 + n, cmp); int ans = inf; int l1 = 1, r1 = 0, l2 = 1, r2 = 0; for (int i = 1, r = 0; i <= n; i++) { while (l1 <= r1 && dq1[l1] < i) l1++; while (l2 <= r2 && dq2[l2] < i) l2++; while (a[dq1[l1]].y - a[dq2[l2]].y < D && r < n) { r++; while (a[dq1[r1]].y <= a[r].y && l1 <= r1) r1--; dq1[++r1] = r; while (a[dq2[r2]].y >= a[r].y && l2 <= r2) r2--; dq2[++r2] = r; } if (l1 <= r1 && l2 <= r2 && a[dq1[l1]].y - a[dq2[l2]].y >= D) ans = min(ans, a[r].x - a[i].x); } if (ans >= inf) puts("-1"); else printf("%d\n", ans); return 0; }
skr