HDU 3410 && POJ 3776 Passing the Message 单调队列
题意:
给定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 && POJ 3776 Passing the Message 单调队列的更多相关文章
- hdu 3410 Passing the Message(单调队列)
题目链接:hdu 3410 Passing the Message 题意: 说那么多,其实就是对于每个a[i],让你找他的从左边(右边)开始找a[j]<a[i]并且a[j]=max(a[j])( ...
- POJ 2823 Sliding Window 【单调队列】
题目链接:http://poj.org/problem?id=2823 题目大意:给出一组数,一个固定大小的窗体在这个数组上滑动,要求出每次滑动该窗体内的最大值和最小值. 这就是典型的单调队列,单调队 ...
- HDU 4193 Non-negative Partial Sums(想法题,单调队列)
HDU 4193 题意:给n个数字组成的序列(n <= 10^6).求该序列的循环同构序列中,有多少个序列的随意前i项和均大于或等于0. 思路: 这题看到数据规模认为仅仅能用最多O(nlogn) ...
- POJ 2823 Sliding Window(单调队列入门题)
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 67218 Accepted: 190 ...
- HDU 4123 Bob's Race:树的直径 + 单调队列 + st表
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4123 题意: 给你一棵树,n个节点,每条边有长度. 然后有m个询问,每个询问给定一个q值. 设dis[ ...
- poj 3250 Bad Hair Day(单调队列)
题目链接:http://poj.org/problem?id=3250 思路分析:题目要求求每头牛看见的牛的数量之和,即求每头牛被看见的次数和:现在要求如何求出每头牛被看见的次数? 考虑到对于某头特定 ...
- Passing the Message 单调栈两次
What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flower” kindergarten ar ...
- HDU 5945 / BestCoder Round #89 1002 Fxx and game 单调队列优化DP
Fxx and game 问题描述 青年理论计算机科学家Fxx给的学生设计了一款数字游戏. 一开始你将会得到一个数\:XX,每次游戏将给定两个参数\:k,tk,t, 任意时刻你可以对你的数执行下面 ...
- poj 2823 Sliding Window(单调队列)
/* 裸地单调队列.. 第一次写 写的好丑.... */ #include<iostream> #include<cstdio> #include<cstring> ...
随机推荐
- MySQL基础之第4章 MySQL数据类型
4.1.整数类型 tinyint(4)smallint(6)mediumint(9)int(11)bigint(20) 注意:后面的是默认显示宽度,以int为例,占用的存储字节数是4个,即4*8=32 ...
- Conversion to Dalvik format failed: Unable to execute dex: null
[2013-11-19 14:18:48 - Dex Loader] Unable to execute dex: java.nio.BufferOverflowException. Check th ...
- How to Calculate difference between two dates in C# z
Do you need to find the difference in number of days, hours or even minute between the two date rang ...
- (转载) 数组a[]={3,5,2,4,1,8},要求从a中找出所有“和”等于10的子集
背包问题. 不过就这道题目本身而言,由于集合a中只要6个元素,而不是成千上万,所以可以使用更直观的办法: 只要你能通过程序给出数组a中元素所组成的集合的所有的子集合(幂集),那么只需在 ...
- 11、WebView 使用总结
<WebView android:id="@+id/webview" android:background="@color/white" android: ...
- STM32 TIM重映射
复用功能 没有重映射 部分重映射 完全重映射 TIM3_CH1 PA6 PB4 PC6 CH2 PA7 PB5 PC7 CH3 PB0 PB0 PC8 CH4 PB1 PB1 PC9 /**重映射 t ...
- 【数据结构和算法】 O(1)时间取得栈中的最大 / 最小元素值
常数时间取得栈中的元素最大值和最小值,我们可以想到当push的时候比较一下,如果待push元素值小于栈顶元素,则更新min值,最大值亦然. 这样有个问题就是当pop的时候,就没了最大最小值. 于是上网 ...
- maven学习系列教程,第一课(web项目的搭建)
1.现在一般eclipse都已经装好了maven板块,无需自行下载安装,所以我们的第一步就是新建一个maven project 2地址使用默认的就行 3这边筛选一下,选择webapp 4. 5.建好后 ...
- 解决PHP5.3.x下ffmpeg安装配置问题
本人的环境: OS : windows 7 64位 WAMP:2.1a PHP:5.3.3(之前是5.3.13) 项目需要用ffmpeg-php实现上传视频转码截图等功能,但是找了很多资料都没有把ff ...
- leetcode—Valid Parentheses
1.问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if t ...