Spoj-DRUIDEOI Fata7y Ya Warda!
Fata7y Ya Warda!
Druid (AKA Amr Alaa El-Deen) and little EOIers have finished their training and they are playing "Fatta7y ya warda!". It's a kids game when everyone holds hands with two other kids forming a circle, and they keep saying "Fatta7y ya warda!" (Flourish, flower!) (moving away from each other, while still holding hands, to form a huge circle), then "2affely ya warda!" (Die, flower!) (moving back as close to each other as possible, while still holding hands, to form a tiny circle, i.e. a point). That's it!
Anyway the point is...
While Eagle (AKA Mohamed Ahmed) was watching them playing he was wondering, who's the first person taller than Druid on his left? Similarly, who's the first person taller than Druid on his right? Help Eagle find the answer for each person not just Druid.
Input
The input starts with an integer T (1 ≤ T ≤ 20), the number of test cases.
Each test case contains two lines. The first line contains a single integer N (1 ≤ N ≤ 105), the number of persons playing the game. The second line contains N integers hi (1 ≤ hi ≤ 109) the height of the i-th person. They are numbered 1 to N starting from Druid.
Output
For each test case print N lines, in the i-th line print 2 numbers, the index of the first person taller than the i-th person on his left, and the index of the first person taller than the i-th person on his right. If no one is taller than the i-th person print -1 -1.
Example
Input:
3
5
172 170 168 171 169
3
172 169 172
1
172
Output:
-1 -1
1 4
2 4
1 1
4 1
-1 -1
1 3
-1 -1
-1 -1 首先这是一个环不是链(我第一次就是wa在这里)
找到每个点左右距离最近的,严格比它矮的第一个人编号
然后就是左右单调递减栈搞一搞,因为是个环所以要长度复制一倍
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,top;
int zhan[];
int a[];
int l[],r[];
int main()
{
int T=read();
while (T--)
{
n=read();for (int i=;i<=n;i++)a[i]=a[i+n]=a[i+*n]=read();
top=;
for (int i=;i<=*n;i++)
{
while (top&&a[zhan[top]]<=a[i])top--;
l[i]=top?zhan[top]:-;
zhan[++top]=i;
}
top=;
for (int i=*n;i>=;i--)
{
while (top&&a[zhan[top]]<=a[i])top--;
r[i]=top?zhan[top]:-;
zhan[++top]=i;
}
for (int i=;i<=n;i++)printf("%d %d\n",l[i+n]>n?l[i+n]-n:l[i+n],r[i]>n?r[i]-n:r[i]);
}
}
Spoj DRUIDEOI
Spoj-DRUIDEOI Fata7y Ya Warda!的更多相关文章
- Fata7y Ya Warda! SPOJ - DRUIDEOI 单调栈
题意:1e5个数围成一个环.现在要输出每个数左右第一个大于它的数的下标.若没有,则输出-1. 题解:单调栈板题.只是要把数据压入栈压两遍来模仿环. 具体分析:考虑一个递减的数列.要找左边最大的 ...
- SPOJDRUIDEOI - Fata7y Ya Warda!【单调栈】
题目链接[http://www.spoj.com/problems/DRUIDEOI/en/] 题意:给出n个数,从1到n围城一个环(1和n相连),求每个数左边第一个比他大的第一个下标,右边第一个比他 ...
- SPOJ - AMR11B
题目链接:https://www.spoj.com/problems/AMR11B/en/ 题目大意就是要你求图形覆盖的格点数,标记每个图形里的未标记格点(包括边界),总标记数就是覆盖的总格点数. # ...
- SPOJ CIRU SPOJ VCIRCLE 圆的面积并问题
SPOJ VCIRCLE SPOJ CIRU 两道题都是给出若干圆 就面积并,数据规模和精度要求不同. 求圆面积并有两种常见的方法,一种是Simpson积分,另一种是几何法. 在这里给出几何方法. P ...
- SPOJ GSS8 - Can you answer these queries VIII | 平衡树
题目链接 这一道题的修改操作用平衡树都很容易实现,难处理的是询问操作. 要想解决询问操作,只要知道如何在平衡树上快速合并左右两个区间的答案即可. 设$Ans_{[l,r]}^k=\sum\limits ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- 【填坑向】spoj COT/bzoj2588 Count on a tree
这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
随机推荐
- A*算法研究
许多工业与科学计算问题都可以转化为在图中寻路问题.启发式的寻路方法将问题表示为一个图,然后利用问题本身的信息,来加速解的搜索过程.一个典型的例子是有一些通路连接若干城市,找出从指定起点城市到指定终点城 ...
- kafka 安装以及测试
1,下载kafka 并进行解压 http://mirrors.cnnic.cn/apache/kafka/0.8.1.1/kafka_2.9.2-0.8.1.1.tgz 2,启动Zookeeper ...
- 第009课 gcc和arm-linux-gcc和MakeFile
from:第009课 gcc和arm-linux-gcc和MakeFile 第001节_gcc编译器1_gcc常用选项_gcc编译过程详解 gcc的使用方法 gcc [选项] 文件名 gcc常用选项 ...
- 小白安装python软件
首先下载:anaconda3.x 下载方式:百度搜索 清华镜像anaconda https://mirrors.tuna.tsinghua.edu.cn/help/anacond ...
- call和apply方法的异同
基本作用:改变对象的执行上下文. this指向执行上下文.(执行环境) this指向的永远是调用该方法的对象 function func(){ this.a=1; console.log(this.a ...
- java获取本地计算机MAC地址
java获取本地计算机MAC地址代码如下: public class SocketMac { //将读取的计算机MAC地址字节转化为字符串 public static String transByte ...
- 【windows】【php】【nginx】windows 开机自启动nginx php 及nginx php配置
#启动php-nginx start-php-nginx.bat @ECHO OFFECHO Starting PHP FastCGI...RunHiddenConsole.exe php-c ...
- 【支付宝支付】扫码付和app支付,回调验证签名失败问题
在检查了参数排序,编码解码,文件编码等问题后,发现还是签名失败,最后找出原因: 扫码付和app支付采用的支付宝公钥不一样 Pid和公钥管理里面: 开放平台密钥界面和开放平台应用界面的密钥应该一 ...
- php各种主流框架的优缺点总结
ThinkPHP ThinkPHP(FCS)是一个轻量级的中型框架,是从Java的Struts结构移植过来的中文PHP开发框架.它使用面向对象的开发结构和MVC模式,并且模拟实现了Struts的标签库 ...
- Python之路-基础数据类型之列表 元组
列表的定义 列表是Python基础数据类型之一,它是以[ ]括起来, 每个元素用' , '隔开而且可以存放各种数据类型: lst = [1,2,'你好','num'] 列表的索引和切片 与字符串类似, ...