hdu3410 单调队列
Passing the Message
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 547 Accepted Submission(s): 344
Because all kids have different height, Teacher Liu set some message passing rules as below:
1.She tells the message to the tallest kid.
2.Every kid who gets the message must retell the message to his “left messenger” and “right messenger”.
3.A kid’s “left messenger” is the kid’s tallest “left follower”.
4.A kid’s “left follower” is another kid who is on his left, shorter than him, and can be seen by him. Of course, a kid may have more than one “left follower”.
5.When a kid looks left, he can only see as far as the nearest kid who is taller than him.
The definition of “right messenger” is similar to the definition of “left messenger” except all words “left” should be replaced by words “right”.
For example, suppose the height of all kids in the row is 4, 1, 6, 3, 5, 2 (in left to right order). In this situation , teacher Liu tells the message to the 3rd kid, then the 3rd kid passes the message to the 1st kid who is his “left messenger” and the 5th kid who is his “right messenger”, and then the 1st kid tells the 2nd kid as well as the 5th kid tells the 4th kid and the 6th kid.
Your task is just to figure out the message passing route.
Each test case consists of two lines. The first line is an integer N (0< N <= 50000) which represents the number of kids. The second line lists the height of all kids, in left to right order. It is guaranteed that every kid’s height is unique and less than 2^31 – 1 .
/*
* Author: sweat123
* Created Time: 2016/7/11 20:23:02
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
struct node{
int id;
int val;
}q[MAXN];
int a[MAXN],n,cnt;
int l[MAXN],r[MAXN];
int main(){
int t,ff = ;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i = ; i <= n; i++){
scanf("%d",&a[i]);
}
cnt = ;
for(int i = ; i <= n; i++){
if(cnt == ){
l[i] = ;
q[++cnt].val = a[i];
q[cnt].id = i;
} else{
if(a[i] < q[cnt].val){
l[i] = ;
q[++cnt].val = a[i];
q[cnt].id = i;
} else {
while(cnt && q[cnt].val < a[i]){
cnt --;
}
l[i] = q[cnt+].id;
q[++cnt].val = a[i];
q[cnt].id = i;
}
}
}
cnt = ;
for(int i = n; i >= ; i--){
if(cnt == ){
r[i] = ;
q[++cnt].val = a[i];
q[cnt].id = i;
} else{
if(a[i] < q[cnt].val){
r[i] = ;
q[++cnt].val = a[i];
q[cnt].id = i;
} else{
while(cnt && a[i] > q[cnt].val){
cnt --;
}
r[i] = q[cnt+].id;
q[++cnt].val = a[i];
q[cnt].id = i;
}
}
}
printf("Case %d:\n",++ff);
for(int i = ; i <= n; i++){
printf("%d %d\n",l[i],r[i]);
}
}
return ;
}
hdu3410 单调队列的更多相关文章
- BestCoder Round #89 B题---Fxx and game(单调队列)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5945 问题描述 输入描述 输出描述 输入样例 输出样例 题意:中文题,不再赘述: 思路: B ...
- 单调队列 && 斜率优化dp 专题
首先得讲一下单调队列,顾名思义,单调队列就是队列中的每个元素具有单调性,如果是单调递增队列,那么每个元素都是单调递增的,反正,亦然. 那么如何对单调队列进行操作呢? 是这样的:对于单调队列而言,队首和 ...
- FZU 1914 单调队列
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...
- BZOJ 1047 二维单调队列
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1047 题意:见中文题面 思路:该题是求二维的子矩阵的最大值与最小值的差值尽量小.所以可以考 ...
- 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列
第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...
- BZOJ1047: [HAOI2007]理想的正方形 [单调队列]
1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2857 Solved: 1560[Submit][St ...
- hdu 3401 单调队列优化DP
Trade Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- 【转】单调队列优化DP
转自 : http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列是一种严格单调的队列,可以单调递增,也可以单调递减.队 ...
- hdu3530 单调队列
Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
随机推荐
- 第六章、Struts2数据校验
一.三种实现方式 ① 用validate()方法实现数据校验 继承ActionSupport类,该类实现了Validateable接口,该接口中定义了一个validate()方法,在自定义的Actio ...
- 社交化分享SDK for Unity
前言 社交化分享,即分享到社交网络. 本文主要记录的是在Unity集成社交化分享SDK,现主流的分享SDK有如下: 1.友盟社交化分享 for unity 2.ShareSDK分享 for unity ...
- Sublime3安装过程及常用插件安装及常用快捷键
1 先去http://www.sublimetext.com/官网下载软件,然后网上找一个验证码,注册完成. 2 安装Package Control ,Package Control 插件是一个方 ...
- http协议(七)通用首部字段
通用首部字段的意思,就是:请求和响应报文双方都会使用的首部 1.Cache-Control 通过指定它的指令,能操作缓存的工作机制 指令参数是可选的,多个指令通过“,”分隔 Cache-Control ...
- 叫板OpenStack:用Docker实现私有云
看到各大厂商的云主机,会不会觉得高大上?目前大公司的主流方案是OpenStack,比如某个公司的私有云
- SQL SERVER的连接方式
最近在做项目的时候,遇到了SQLSERVER的连接,以前是很模糊的,现在做一个简单的总结. 针对SQL_SERVER,连接指定的方式有两种,一种是Where条件指定方式,另外一种是采用On连指定连接条 ...
- Linux shell运算符
双引号 --使用双引号可以引用除了字符$,`(单反号),\(反斜杠)外的任意字符或者字符串 --echo "参数的个数是$#" 单引号 --单引号与双引号类似,不同的是shell会 ...
- log4j2 与 spring mvc整合
log4j2不仅仅是log4j的简单升级,而是整个项目的重构,官网地址:http://logging.apache.org/log4j/2.x/,大家可以从官网的介绍看出它相比log4j第1代的种种优 ...
- list使用例子(转)
例子: 在vs2010中创建一个winform的解决方案,然后定义一个类Person,Person.cs 的代码如下: using System;using System.Collections.Ge ...
- 【腾讯GAD暑期训练营游戏程序开发】游戏中的动画系统作业
游戏中的动画系统作业说明文档 一.实现一个动画状态机:至少包含3组大的状态节点