P2698-花盆Flowerpot【单调队列】
生活随笔
收集整理的這篇文章主要介紹了
P2698-花盆Flowerpot【单调队列】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
正題
鏈接
https://www.luogu.org/record/show?rid=7934370
大意
有n滴水,給出坐標,水每一個時間單位會往下掉一格,花盆可以隨意擺放,要求在寬度最小的情況下接住的第一滴水和最后一滴水時間差超過D
解題思路
橫坐標排序,枚舉左邊,然后維護移動窗口,之后遞增右邊直到時間差大于D,然后記錄取最小值。
代碼
#include<cstdio> #include<algorithm> using namespace std; struct node{int x,t; }a[100001]; int h1,t1,h2,t2,q[100001],q2[100001]; int n,D,ans,r; bool cmp(node x,node y)//排序 {return x.x<y.x; } int main() {scanf("%d%d",&n,&D);for (int i=1;i<=n;i++){scanf("%d%d",&a[i].x,&a[i].t);}sort(a,a+1+n,cmp);h1=1;t1=0;ans=2147483647;h2=1;t2=0;r=0;for (int l=1;l<=n;l++){while (h1<=t1&&q[h1]<l) h1++;while (h2<=t2&&q2[h2]<l) h2++;//維護移動窗口while (a[q[h1]].t-a[q2[h2]].t<D&&r<n){r++;//尋找右端點while (h1<=t1&&a[r].t>a[q[t1]].t) t1--;while (h2<=t2&&a[r].t<a[q2[t2]].t) t2--;//維護單調隊列q[++t1]=q2[++t2]=r;//加入隊列}if (a[q[h1]].t-a[q2[h2]].t>=D)ans=min(ans,a[r].x-a[l].x);//更新最小值}if (ans==2147483647) printf("-1");else printf("%d",ans); }總結
以上是生活随笔為你收集整理的P2698-花盆Flowerpot【单调队列】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: P2055-假期的宿舍【网络流,最大流,
- 下一篇: 蛋糕不够蓬松是因为什么原因 蛋糕不够蓬松