HDU 3410 && POJ 3776 Passing the Message 单调队列
来源:互联网 发布:音速启动是什么软件 编辑:程序博客网 时间:2024/04/30 08:18
题意:
给定n长的数组(下标从1-n)(n个人的身高,身高各不相同
问:对于第i个人,他能看到的左边最矮的人下标。(如果这个最矮的人被挡住了,则这个值为0)
还有右边最高的人下标,同理若被挡住了则这个值为0
输出n行,每个人左右2边的下标。
单调队列,对于 front - rear 的队列(注意出队都是在rear,入队也是在rear)
当加入元素x,若这队列是单调递增的,显然q.front() <= x , 反之若>x ,则把队首元素弹掉,这样就能保持单调性。
若弹掉了队首元素,在此题中就相当于最矮的人被挡住了,因为{q.end() < q.front() }> x。
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <queue>#include <set>#include <map>using namespace std;#define N 50010int l[N], r[N];int a[N],n;int L,R;int q[N*2];int pos[N];void init(){ L=1;R=0; }void work1(){init();for(int i = 1; i <= n; i++){bool fa = false;while(L<=R && q[R]<a[i])R--, fa = true;if(fa) l[i] = pos[q[R+1]];else l[i] = 0;q[++R] = a[i];}}void work2(){init();for(int i = n; i; i--){bool fa = false;while(L<=R && q[R]<a[i])R--, fa = true;if(fa) r[i] = pos[q[R+1]];else r[i] = 0;q[++R] = a[i];}}map<int,int>mp;int Stack[N];int main(){int i,j ,u, v, T, Cas = 1;scanf("%d",&T);while(T--){mp.clear();scanf("%d",&n);for(i=1;i<=n;i++)scanf("%d",&a[i]);work1();work2();printf("Case %d:\n",Cas++);for(int i = 1;i <=n; i++)printf("%d %d\n",l[i],r[i]);}return 0;}
0 0
- HDU 3410 && POJ 3776 Passing the Message 单调队列
- hdu-3410 Passing the Message[单调队列]
- HDU 3410 Passing the Message (单调栈)
- HDU3410 Passing the Message(单调队列模板)
- 文章标题 HDU 3410 : Passing the Message (单调栈)
- POJ 3776 Passing the Message
- HDU 3410Passing the Message
- HDU 3410 Passing the Message
- hdu 3410 Passing the Message
- HDU 3410 Passing the Message
- HDU 3410 Passing the Message
- HDU3410 Passing the Message (单调队列,线段树可解)
- Passing the Message(单调栈)
- hdu3410 Passing the Message 单调栈
- Passing the Message (单调栈)
- hdu3410 Passing the Message 单调栈
- Passing the Message
- Passing the Message
- oc 通知中心
- 快速排序算法
- 华为编程大赛 每日一练(二)
- android的消息处理机制(图+源码分析)——Looper,Handler,Message+10
- Permutation Sequence - 20 mins
- HDU 3410 && POJ 3776 Passing the Message 单调队列
- How to add custom title bar to android application
- Windows 7 or 8 Master Boot Record (MBR)
- 【操作系统】线程实现方式(内核级线程、用户级线程)
- UVa 11401 11401 - Triangle Counting
- oracle 系统默认的用户及密码
- Linux 系统挂载数据盘教程
- java Applet绘图。。。
- 初观设计模式