HDU 3410【单调栈】
思路:
单调栈。
鄙人的记忆:按当前为最大值的两边延伸就是维护单调递减栈。
//#include <bits/stdc++.h>
#include <iostream>
#include <stack>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=50000+10;
struct asd{
int id;
int left,right,w;
};
int n,a[N];
int ans[N][2];
stack<asd>q;
int main()
{
while(!q.empty())
q.pop();
int T,cas=1;
scanf("%d",&T);
while(T--)
{
asd now,nex;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
now.id=1;
now.left=now.right=1;
now.w=a[1];
q.push(now);
for(int i=2;i<=n;i++)
{
nex.id=i;
nex.left=nex.right=i;
nex.w=a[i];
while(!q.empty()&&q.top().w<a[i])
{
now=q.top();q.pop();
ans[now.id][0]=now.left!=now.id?now.left:0;
ans[now.id][1]=now.right!=now.id?now.right:0;
if(!q.empty())
q.top().right=now.id;
nex.left=now.id;
}
q.push(nex);
}
while(!q.empty())
{
now=q.top();q.pop();
ans[now.id][0]=now.left!=now.id?now.left:0;
ans[now.id][1]=now.right!=now.id?now.right:0;
if(!q.empty())
q.top().right=now.id;
}
printf("Case %d:\n",cas++);
for(int i=1;i<=n;i++)
printf("%d %d\n",ans[i][0],ans[i][1]);
}
return 0;
}
HDU 3410【单调栈】的更多相关文章
- hdu 3410 单调栈
http://acm.hdu.edu.cn/showproblem.php?pid=3410 Passing the Message Time Limit: 2000/1000 MS (Java/Ot ...
- hdu 1506 单调栈问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目的意思其实就是要找到一个尽可能大的矩形来完全覆盖这个矩形下的所有柱子,只能覆盖柱子,不能留空 ...
- hdu 5033 单调栈 ****
看出来是单调栈维护斜率,但是不会写,2333,原来是和询问放在一起的 #include <iostream> #include <cstdio> #include <cs ...
- hdu 5875(单调栈)
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU 5033 (单调栈维护凸包) Building
题意: 一个人在x轴上,他的左右两侧都有高楼,给出楼的横坐标Xi和高度Hi还有人的位置pos,求人所能看到的天空的最大角度. 分析: 将建筑物和人的位置从左到右排序,对于每个位置利用栈求一次人左边建筑 ...
- hdu 4923 单调栈
http://acm.hdu.edu.cn/showproblem.php?pid=4923 给定一个序列a,元素由0,1组成,求一个序列b,元素在0~1之间,并且保证递增.输出最小的∑(ai−bi) ...
- hdu 1505 单调栈升级版
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #defin ...
- hdu 1506 单调栈
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #defin ...
- hdu 3410 Passing the Message(单调队列)
题目链接:hdu 3410 Passing the Message 题意: 说那么多,其实就是对于每个a[i],让你找他的从左边(右边)开始找a[j]<a[i]并且a[j]=max(a[j])( ...
随机推荐
- centos设置固定IP方法
首先网络模式设为桥接 [root@centos64 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0HWADDR=00:0C:2 ...
- web前端开发-Ajax(1)
1.简单简绍Ajax的功能 Ajax是处于前端和后端之间的这么一个东西,他可以拿到你前端form的内容,并且在你触发Ajax的时候,先将某些数据发送到服务器端,等接受到服务器 返回的数据时,执行某个函 ...
- matlab vs使用
~ matlab无论什么程序只输出 ans 1 注意matlab命名规则:1不能与matlab内部函数名字重合.2.文件名首字母不能是数字或下划线.3.文件名中不能有空格.4.文件名不能太长.5注意大 ...
- POJ 之 WERTYU
WERTYU Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8371 Accepted: 4007 Descriptio ...
- 搭建LoadRunner中的场景(三)场景的执行计划
所谓场景操作,包括初始化用户组.启动用户组各用户以及停止虚拟用户的全过程.依据设置不同,执行过程中可以最多有5类操作,分别是启动用户组(start group).初始化(Initialize).启动虚 ...
- MySQL存储过程示例
MySQL存储过程: /*自定义结束符*/ DELIMITER $$ /*如果存在同名的存储过程就删除*/ DROP PROCEDURE IF EXISTS prAddBlack$$ /*创建存储过程 ...
- linux命令学习笔记(45):free 命令
free命令可以显示Linux系统中空闲的.已用的物理内存及swap内存,及被内核使用的buffer.在Linux系统监控的 工具中,free命令是最经常使用的命令之一. .命令格式: free [参 ...
- linux命令学习笔记(29):chgrp命令
在lunix系统里,文件或目录的权限的掌控以拥有者及所诉群组来管理.可以使用chgrp指令取变更文件与目录所属群 组,这种方式采用群组名称或群组识别码都可以.Chgrp命令就是change group ...
- ACM学习历程—HDU4956 Poor Hanamichi(模拟)
Poor Hanamichi Problem Description Hanamichi is taking part in a programming contest, and he is assi ...
- bzoj 2251: 外星联络 后缀Trie
题目大意 http://www.lydsy.com/JudgeOnline/problem.php?id=2251 题解 本来以为这道题应该从01序列的性质入手 结果就想歪了 等自己跳出了01序列这个 ...