NC24325 [USACO 2012 Mar S]Flowerpot

题目

题目描述

Farmer John has been having trouble making his plants grow, and needs your help to water them properly. You are given the locations of N raindrops (1 <= N <= 100,000) in the 2D plane, where y represents vertical height of the drop, and x represents its location over a 1D number line:

Each drop falls downward (towards the x axis) at a rate of 1 unit per second. You would like to place Farmer John's flowerpot of width W somewhere along the x axis so that the difference in time between the first raindrop to hit the flowerpot and the last raindrop to hit the flowerpot is at least some amount D (so that the flowers in the pot receive plenty of water). A drop of water that lands just on the edge of the flowerpot counts as hitting the flowerpot.

Given the value of D and the locations of the N raindrops, please compute the minimum possible value of W.

输入描述

  • Line 1: Two space-separated integers, N and D. (1 <= D <=

    1,000,000)

  • Lines 2..1+N: Line i+1 contains the space-separated (x,y)

    coordinates of raindrop i, each value in the range

    0...1,000,000.

输出描述

  • Line 1: A single integer, giving the minimum possible width of the

    flowerpot. Output -1 if it is not possible to build a

    flowerpot wide enough to capture rain for at least D units of

    time.

示例1

输入

4 5
6 3
2 4
4 10
12 15

输出

2

说明

INPUT DETAILS:

There are 4 raindrops, at (6,3), (2,4), (4,10), and (12,15). Rain must

fall on the flowerpot for at least 5 units of time.

OUTPUT DETAILS:

A flowerpot of width 2 is necessary and sufficient, since if we place it

from x=4..6, then it captures raindrops #1 and #3, for a total rain

duration of 10-3 = 7.

题解

思路

方法一

知识点:单调队列,二分。

求可行答案的最小值,第一个想到的就是二分答案,检验答案是否可行。

验证一个区间是否可行,首先要知道一个区间最大最小值,但是这个区间是可变的,考虑用单调队列维护一个定长移动区间最大最小值。

细节上要注意,由于区间长度是非线性变化的,所以用while和两个端点指针来保证每一个区间是 \(<=mid\) 最大区间。并且队头弹出操作,队尾加入新元素操作,都是需要一个while维持的,因为不保证合法区间需要操作几个点。初始化时,要在 \(<=mid\) 最大区间之前保留一个点,方便之后遍历是从第一个区间开始的。因为检验可行性就行,所以遇到一个可行就可以跳出了。

时间复杂度 \(O(n \log x_{max})\)

空间复杂度 \(O(n)\)

方法二

知识点:尺取法,单调队列。

首先,如果右端点找到一个合法区间,改变左端点时,不需要将右端点重置到左端点处,因为首末雨滴时差随区间变大是只增不减的,所以右端点一直往右即可,符合尺取法。

维护变区间最大最小值用单调队列即可,细节如上。

注意到,由于内循环需要一开始就检测是否已经合法,而不是加入后检测,因为为了固定右端点,改变左端点得到的合法区间都取到。因此为了防止空队列被访问,一开始加入一个点使得队列持续非空即可。

每次和答案取最小值时,要在前面加一个判断是否合法,因为可能导致内循环跳出的不是区间合法而是右端点到头了。

时间复杂度 \(O(n)\)

空间复杂度 \(O(n)\)

代码

方法一

#include <bits/stdc++.h>

using namespace std;

struct Point {
int x, y;
}a[1000007];
int n, d; bool check(int mid) {
deque<int> dq1, dq2;
int l = 0, r = 0;
while (r < n - 1 && a[r + 1].x - a[0].x <= mid) {///单调递增/递减队列初始化,以获得所有长为mid的区间的最小值/最大值
while (!dq1.empty() && a[dq1.back()].y >= a[r].y) dq1.pop_back();
dq1.push_back(r);
while (!dq2.empty() && a[dq2.back()].y <= a[r].y) dq2.pop_back();
dq2.push_back(r);
r++;
}
int cnt = 0;
while (r < n) {
while (a[r].x - a[l].x > mid) l++;
while (!dq1.empty() && dq1.front() < l) dq1.pop_front();
while (!dq2.empty() && dq2.front() < l) dq2.pop_front();
while (r < n && a[r].x - a[l].x <= mid) {
while (!dq1.empty() && a[dq1.back()].y >= a[r].y) dq1.pop_back();
dq1.push_back(r);
while (!dq2.empty() && a[dq2.back()].y <= a[r].y) dq2.pop_back();
dq2.push_back(r);
r++;
}
if (a[dq2.front()].y - a[dq1.front()].y >= d) return true;
}
return false;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> d;
for (int i = 0;i < n;i++) cin >> a[i].x >> a[i].y;
sort(a, a + n, [&](Point a, Point b) {return a.x == b.x ? a.y < b.y : a.x < b.x;}); int l = 1, r = a[n - 1].x;
while (l <= r) {
int mid = l + r >> 1;
if (check(mid)) r = mid - 1;
else l = mid + 1;
}
cout << (l > a[n - 1].x ? -1 : l) << '\n';
return 0;
}

方法二

///注意边界,可以通过初始化减少if语句长度
#include <bits/stdc++.h> using namespace std; struct Point {
int x, y;
}a[1000007]; int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, d;
cin >> n >> d;
for (int i = 0;i < n;i++) cin >> a[i].x >> a[i].y;
sort(a, a + n, [&](Point a, Point b) {return a.x == b.x ? a.y < b.y : a.x < b.x;}); deque<int> dq1, dq2;
dq1.push_back(0);
dq2.push_back(0);
int l = 0, r = 1;
int ans = ~(1 << 31);
while (l < n) {
while (r < n) {
if (a[dq2.front()].y - a[dq1.front()].y >= d) break;
while (!dq1.empty() && a[dq1.back()].y >= a[r].y) dq1.pop_back();
dq1.push_back(r);
while (!dq2.empty() && a[dq2.back()].y <= a[r].y) dq2.pop_back();
dq2.push_back(r);
r++;
}
if (a[dq2.front()].y - a[dq1.front()].y >= d) ans = min(ans, a[r - 1].x - a[l].x);
if (l == dq1.front()) dq1.pop_front();
if (l == dq2.front()) dq2.pop_front();
l++;
}
cout << (ans > a[n - 1].x ? -1 : ans) << '\n';
return 0;
}

NC24325 [USACO 2012 Mar S]Flowerpot的更多相关文章

  1. [USACO 2012 Mar Silver] Landscaping【Edit Distance】

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=126 好题啊好题,一开始就输给了这道题的想法! 先把原始状态以及目标状态换 ...

  2. [USACO 2012 Mar Gold] Large Banner

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=127 又是一道这种题目,遇到一次跪一次,这次终于硬着头皮看懂了题解,但是谢 ...

  3. USACO翻译:USACO 2012 FEB Silver三题

    USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...

  4. USACO翻译:USACO 2012 JAN三题(2)

    USACO 2012 JAN(题目二) 一.题目概览 中文题目名称 叠干草 分干草 奶牛联盟 英文题目名称 stacking baleshare cowrun 可执行文件名 stacking bale ...

  5. USACO翻译:USACO 2012 JAN三题(1)

    USACO 2012 JAN(题目一) 一.题目概览 中文题目名称 礼物 配送路线 游戏组合技 英文题目名称 gifts delivery combos 可执行文件名 gifts delivery c ...

  6. 【USACO 2012 Open】Running Laps(树状数组)

    53 奶牛赛跑 约翰有 N 头奶牛,他为这些奶牛准备了一个周长为 C 的环形跑牛场.所有奶牛从起点同时起跑,奶牛在比赛中总是以匀速前进的,第 i 头牛的速度为 Vi.只要有一头奶牛跑完 L 圈之后,比 ...

  7. NC24840 [USACO 2009 Mar S]Look Up

    NC24840 [USACO 2009 Mar S]Look Up 题目 题目描述 Farmer John's N (1 <= N <= 100,000) cows, convenient ...

  8. USACO翻译:USACO 2012 JAN三题(3)

    USACO 2012JAN(题目三) 一.题目概览 中文题目名称 放牧 登山 奶牛排队 英文题目名称 grazing climb lineup 可执行文件名 grazing climb lineup ...

  9. USACO 2012 Feb Cow Coupons

    2590: [Usaco2012 Feb]Cow Coupons Time Limit: 10 Sec Memory Limit: 128 MB Submit: 349 Solved: 181 [Su ...

随机推荐

  1. android软件简约记账app开发day08-时间对话框的书写+改bug,改bug

    android软件简约记账app开发day08-时间对话框的书写+改bug,改bug 绘制对话跨页面 在添加记账信息功能中,我提供了用户添加备注添加事件的功能,设计是点击时间会弹出一个时间对话框供用户 ...

  2. 2021.07.17 P4170 染色(区间DP)

    2021.07.17 P4170 染色(区间DP) [P4170 CQOI2007]涂色 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 重点: 1.目标状态可以由哪些状态转移过来. ...

  3. 别像弱智一样提问 Stop-Ask-Questions-The-Stupid-Ways

    https://github.com/xcr1234/Stop-Ask-Questions-The-Stupid-Ways 你真的准备好了吗? 感谢群友 for you 提供 避免 xy-proble ...

  4. Web项目部署指南

    Web项目部署指南 本文记录了部署Vue项目到阿里云服务器上的过程,其中云服务器的操作系统是CentOS 7,Web服务器用的是nginx.因为项目涉及发送异步请求,而由Flask编写的后端应用监听的 ...

  5. 【FAQ】HMS Core推送服务与本地创建通知消息如何相互覆盖?

    我们知道,单独使用HMS Core推送服务或本地创建通知消息,都可以实现通知消息的覆盖,方式分别为: 1.本地创建通知消息(简称本地通知消息) 通过notificationManager.notify ...

  6. Java 18 新特性:使用Java代码启动jwebserver

    前几天分享了Java 18 新特性:简单Web服务器的jwebserver命令行功能. 今天换一种方式,使用Java代码来实现一个静态资源服务器. 详细步骤我录了个视频放到B站了,感兴趣的小伙伴可以点 ...

  7. 手脱UPX(3.91)

    1.使用Detect It Easy进行查壳: 2.使用x32dbg打开该带壳程序,在选项->选项->异常中添加区间设置0~FFFFFFFF全忽略: 3.我们F9运行到程序入口处,看到了p ...

  8. ESP32+阿里云+vscode_Pio

    用ESP32在vscode使用PlatformPIO写的代码.(代码是折叠代码,不能一眼瞧见,我也不太会使用编辑器哈,刚写博不久,望谅解.) 功能:esp32联网,能够通过联网打开在阿里云平台控制设备 ...

  9. HDD线上沙龙·创新开发专场:多元服务融合,助力应用创新开发

    5月24日,由华为开发者联盟主办的HUAWEI Developer Day(华为开发者日,简称HDD)线上沙龙·创新开发专场在华为开发者学堂及各大直播平台与广大开发者见面.直播内容主要聚焦Harmon ...

  10. 关于『Markdown』:第二弹

    关于『Markdown』:第二弹 建议缩放90%食用 道家有云:一生二,二生三,三生万物 为什么我的帖子不是这样 各位打工人们! 自从我学了Markdown以来 发现 Markdown 语法真的要比 ...