NC24325. [USACO 2012 Mar S]Flowerpot
描述
输入描述
* 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:C++(clang++ 11.0.1) 解法, 执行用时: 63ms, 内存消耗: 1096K, 提交时间: 2023-01-12 20:00:11
#include <bits/stdc++.h> using namespace std; #define pii pair<int,int> #define x first #define y second const int M=1e5+5; pii a[M],st[M]; int main() { int h=1,t=0,ans=1e9; int n,m; cin>>n>>m; for(int i=1;i<=n;i++) cin>>a[i].x>>a[i].y; sort(a+1,a+1+n); for(int i=1;i<=n;i++) { while(h<=t&&st[t].y>=a[i].y)t--; st[++t]=a[i]; while(st[t].y-st[h].y>=m)ans=min(ans,st[t].x-st[h++].x); } printf("%d",ans==1e9?-1:ans); return 0; }