日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

洛谷P2698 [USACO12MAR]花盆Flowerpot

發(fā)布時(shí)間:2025/3/21 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 洛谷P2698 [USACO12MAR]花盆Flowerpot 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

P2698 [USACO12MAR]花盆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.

老板需要你幫忙澆花。給出N滴水的坐標(biāo),y表示水滴的高度,x表示它下落到x軸的位置。

每滴水以每秒1個(gè)單位長(zhǎng)度的速度下落。你需要把花盆放在x軸上的某個(gè)位置,使得從被花盆接著的第1滴水開(kāi)始,到被花盆接著的最后1滴水結(jié)束,之間的時(shí)間差至少為D。

我們認(rèn)為,只要水滴落到x軸上,與花盆的邊沿對(duì)齊,就認(rèn)為被接住。給出N滴水的坐標(biāo)和D的大小,請(qǐng)算出最小的花盆的寬度W。

輸入輸出格式

輸入格式:

第一行2個(gè)整數(shù) N 和 D。

第2.. N+1行每行2個(gè)整數(shù),表示水滴的坐標(biāo)(x,y)。

輸出格式:

僅一行1個(gè)整數(shù),表示最小的花盆的寬度。如果無(wú)法構(gòu)造出足夠?qū)挼幕ㄅ?#xff0c;使得在D單位的時(shí)間接住滿足要求的水滴,則輸出-1。

輸入輸出樣例

輸入樣例#1:?
4 5 6 3 2 4 4 10 12 15 輸出樣例#1:?
2

說(shuō)明

【樣例解釋】

有4滴水, (6,3), (2,4), (4,10), (12,15).水滴必須用至少5秒時(shí)間落入花盆。花盆的寬度為2是必須且足夠的。把花盆放在x=4..6的位置,它可以接到1和3水滴, 之間的時(shí)間差為10-3 = 7滿足條件。

【數(shù)據(jù)范圍】

40%的數(shù)據(jù):1 ≤ N ≤ 1000,1 ≤ D ≤ 2000;

100%的數(shù)據(jù):1 ≤ N ≤ 100000,1 ≤ D ≤ 1000000,0≤x,y≤10^6。

?

sol:單調(diào)隊(duì)列蠻顯然的吧(其實(shí)接近板子題)

維護(hù)一個(gè)遞增的單調(diào)隊(duì)列,當(dāng)隊(duì)尾與隊(duì)首的高度差>D時(shí)彈隊(duì)首,當(dāng)隊(duì)尾比當(dāng)前元素大時(shí)彈隊(duì)尾

#include <bits/stdc++.h> using namespace std; typedef long long ll; inline ll read() {ll s=0;bool f=0;char ch=' ';while(!isdigit(ch)){f|=(ch=='-'); ch=getchar();}while(isdigit(ch)){s=(s<<3)+(s<<1)+(ch^48); ch=getchar();}return (f)?(-s):(s); } #define R(x) x=read() inline void write(ll x) {if(x<0){putchar('-'); x=-x;}if(x<10){putchar(x+'0'); return;}write(x/10);putchar((x%10)+'0');return; } #define W(x) write(x),putchar(' ') #define Wl(x) write(x),putchar('\n') const int N=100005,inf=0x3f3f3f3f; int n,D; struct Shuidi {ll X,Y; }Water[N]; inline bool cmp(Shuidi p,Shuidi q) {return p.X<q.X; } struct Record {ll Weiz,Shuz; }Ddq[N]; int main() {int i,j;R(n); R(D);for(i=1;i<=n;i++){int x=read(),y=read();Water[i]=(Shuidi){x,y};}sort(Water+1,Water+n+1,cmp);int Head=1,Tail=0;ll ans=inf;for(i=1;i<=n;i++){while(Head<Tail&&Water[Ddq[Head+1].Weiz].Y+D<=Water[Ddq[Tail].Weiz].Y) Head++;if(Water[Ddq[Head].Weiz].Y+D<=Water[Ddq[Tail].Weiz].Y) ans=min(ans,Ddq[Tail].Shuz-Ddq[Head].Shuz);while(Head<=Tail&&Water[Ddq[Tail].Weiz].Y>Water[i].Y) Tail--;Ddq[++Tail]=(Record){i,Water[i].X};}if(ans==inf) puts("-1");else Wl(ans);return 0; } /* input 4 5 6 3 2 4 4 10 12 15 output 2 */ View Code

?

轉(zhuǎn)載于:https://www.cnblogs.com/gaojunonly1/p/10579836.html

總結(jié)

以上是生活随笔為你收集整理的洛谷P2698 [USACO12MAR]花盆Flowerpot的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。