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(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
随机推荐
- 洛谷 P1901 发射站
题目描述 某地有 N 个能量发射站排成一行,每个发射站 i 都有不相同的高度 Hi,并能向两边(当 然两端的只能向一边)同时发射能量值为 Vi 的能量,并且发出的能量只被两边最近的且比 它高的发射站接 ...
- bzip2命令
bzip2命令——压缩文件 命令所在路径:/usr/bin/bzip2 示例1: # bzip2 yum.log 压缩当前目录下yum.log文件成yum.log.bz2 示例2: # bzip2 - ...
- Python 字符编码问题的处理
python中的字符编码问题往往是初学者容易弄不明白的问题, 要想将这个问题搞清楚,需要先弄明白以下的概念 decode 和 encode 函数的作用 字符串字面量的编码格式 decode(str) ...
- window10系统安装Ubuntu18.04系统
写这篇博客整理一下使用虚拟机安装Ubuntu系统,一般常用的虚拟机有VMware以及VirtualBox.鉴于方便,博主用的是virtualbox,虽然不是很美观,但简洁,且完全免费,且不需要在自己配 ...
- python小括号( )与中括号 [ ]
在python中小括号()表示的是tuple元组数据类型,元组是一种不可变序列. >>> a = (1,2,3) >>> a (1, 2, 3) >>& ...
- Mysql,SqlServer,Oracle主键自动增长的设置
在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值.例如: CREATE TABLE google(id INT AUTO_INCREMENT PRIMARY ...
- OS X快捷键小技巧
退出command+Q,关分页Command+W,刷新Command+R,新开分页Command+T 全屏 ctrl+command+F 每个Mac使用者都知道点击下窗口左上角黄色圆形的按钮就可以最小 ...
- 8 Java 归并排序(MergerSort)
图片素材与文字描述来自:尚硅谷-韩顺平数据结构与算法. 1.基本思想 归并排序是利用归并的思想实现的排序方法,该算法采用经典的分治(divide-and-conquer)策略(分治法将问题分(divi ...
- js 两个数组对象根据账号比较去重,解决直接splice后数组索引改变
目的获取Arr2中不包含在arr1中的对象 根据Account进行比较,如果相等则删除tempArr数组对象. 结果返回张三 var arr1=[{"account":" ...
- mac系统下android studio创建手机模拟器
打开android studio,点击右上角的模拟器图标,打开“Android Virtual Device Manager” 窗口,如下图 点击“Create Virtual Device”,在 ...