[题解]LuoGu2698: [USACO12MAR]花盆Flowerpot


原题传送门

这道题在洛谷难度为蓝题(提高+省选-)个人认为难度普及+够了吧

不难想到单调队列的做法,首先二分答案,对于每个花盆宽度,我们进行O(n)的验证

维护两个单调队列,分别维护纵坐标的最小值与最大值,从左往右枚举横轴坐标,每个时刻都能计算出题目里说的时间差D,与输入的进行比较,若满足要求,则返回true

Code:

/*
q1维护最小值,q2维护最大值
*/
#include <bits/stdc++.h>
#define res register int
#define ll long long
#define maxn 100010
using namespace std;
struct node{
	int x, y;
}a[maxn];
int n, delta, q1[maxn], q2[maxn];

inline int read(){
	int s = 0, w = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
	for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
	return s * w;
}

inline bool cmp(node x, node y){ return x.x < y.x; }


inline int check(int len){
	int h1 = 1, h2 = 1, t1 = 1, t2 = 1; q1[1] = q2[1] = 1;
	for (res i = 2; i <= n + 1; ++ i){
		if (a[i].x != a[i - 1].x){//实时更新
			res j = a[i - 1].x, Min = -1, Max = -1;
			while (h1 <= t1 && j - a[q1[h1]].x > len) ++h1;
			if (h1 <= t1) Min = Min == -1 ? a[q1[h1]].y : min(Min, a[q1[h1]].y);
			while (h2 <= t2 && j - a[q2[h2]].x > len) ++h2;
			if (h2 <= t2) Max = Max == -1 ? a[q2[h2]].y : max(Max, a[q2[h2]].y);
			if (Min != -1 && Max != -1 && Max - Min >= delta) return 1;
		}
		if (i == n + 1) break;
		while (h1 <= t1 && a[i].y <= a[q1[t1]].y) --t1; q1[++t1] = i;
		while (h2 <= t2 && a[i].y >= a[q2[t2]].y) --t2; q2[++t2] = i;
	}
	return 0;
}

int main(){
	n = read(), delta = read();
	for (res i = 1; i <= n; ++ i) a[i].x = read(), a[i].y = read();
	sort(a + 1, a + 1 + n, cmp);
	int l = 0, r = 1000000, ans = -1;
	while (l <= r){
		int mid = (l + r) >> 1;
		if (check(mid)) ans = mid, r = mid - 1; else l = mid + 1;
	}
	printf("%d\n", ans);
	return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值