题意:

给定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 50010
int 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;
}

HDU 3410 &amp;&amp; POJ 3776 Passing the Message 单调队列的更多相关文章

  1. hdu 3410 Passing the Message(单调队列)

    题目链接:hdu 3410 Passing the Message 题意: 说那么多,其实就是对于每个a[i],让你找他的从左边(右边)开始找a[j]<a[i]并且a[j]=max(a[j])( ...

  2. POJ 2823 Sliding Window 【单调队列】

    题目链接:http://poj.org/problem?id=2823 题目大意:给出一组数,一个固定大小的窗体在这个数组上滑动,要求出每次滑动该窗体内的最大值和最小值. 这就是典型的单调队列,单调队 ...

  3. HDU 4193 Non-negative Partial Sums(想法题,单调队列)

    HDU 4193 题意:给n个数字组成的序列(n <= 10^6).求该序列的循环同构序列中,有多少个序列的随意前i项和均大于或等于0. 思路: 这题看到数据规模认为仅仅能用最多O(nlogn) ...

  4. POJ 2823 Sliding Window(单调队列入门题)

      Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 67218   Accepted: 190 ...

  5. HDU 4123 Bob's Race:树的直径 + 单调队列 + st表

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4123 题意: 给你一棵树,n个节点,每条边有长度. 然后有m个询问,每个询问给定一个q值. 设dis[ ...

  6. poj 3250 Bad Hair Day(单调队列)

    题目链接:http://poj.org/problem?id=3250 思路分析:题目要求求每头牛看见的牛的数量之和,即求每头牛被看见的次数和:现在要求如何求出每头牛被看见的次数? 考虑到对于某头特定 ...

  7. Passing the Message 单调栈两次

    What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flower” kindergarten ar ...

  8. HDU 5945 / BestCoder Round #89 1002 Fxx and game 单调队列优化DP

    Fxx and game 问题描述   青年理论计算机科学家Fxx给的学生设计了一款数字游戏. 一开始你将会得到一个数\:XX,每次游戏将给定两个参数\:k,tk,t, 任意时刻你可以对你的数执行下面 ...

  9. poj 2823 Sliding Window(单调队列)

    /* 裸地单调队列.. 第一次写 写的好丑.... */ #include<iostream> #include<cstdio> #include<cstring> ...

随机推荐

  1. kettle连接hadoop&hdfs图文详解

    1 引言: 项目最近要引入大数据技术,使用其处理加工日上网话单数据,需要kettle把源系统的文本数据load到hadoop环境中 2 准备工作: 1 首先 要了解支持hadoop的Kettle版本情 ...

  2. char[]数组与char *指针的区别

    char[]数组与char *指针的区别 问题描述 虽然很久之前有看过关于char指针和char数组的区别,但是当时没有系统的整理,到现在频繁遇到,在string,char[], char *中迷失了 ...

  3. (转载)OC学习篇之---类的定义

    之前已经介绍了OC中的一个程序HelloWorld,今天我们继续学习OC中类的相关知识. OC和C的最大区别就是具有了面向对象的功能,那么说到面向对象,就不得不说类这个概念了,如果学过Java的话,那 ...

  4. Python覆盖率分析工具_Coverage

    easy_install安装: easy_install coverage 运行: coverage run test.py coverage report

  5. 第三百零二天 how can I 坚持

    今天给掌中宝提了几个bug,确实管用,哈哈. 还有就是弟弟买房了,海亮艺术公馆,还好,至少安定下来了,可惜啊,我看好的房子也有的卖了,咋办啊. 看准的东西总是会想法设法的买了,可是无能为力啊. 还有, ...

  6. Yii CModel中rules验证规则

    array( array(‘username’, ‘required’), array(‘username’, ‘length’, ‘min’=>3, ‘max’=>12), array( ...

  7. Spring InitializingBean and DisposableBean example

    In Spring, InitializingBean and DisposableBean are two marker interfaces, a useful way for Spring to ...

  8. POJ Wormholes (SPFA)

    http://poj.org/problem?id=3259 Description While exploring his many farms, Farmer John has discovere ...

  9. [转]freemarker中的list

    转至:http://zhuyuehua.iteye.com/blog/1975251 freemarker list (长度,遍历,下标,嵌套,排序) 1. freemarker获取list的size ...

  10. HTTP Header 简介

    HTTP Header 简介 HTTP(HyperTextTransferProtocol)即超文本传输协议,目前网页传输的的通用协议.HTTP协议采用了请求/响应模型,浏览器或其他客户端发出请求,服 ...