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

题意:有n个人站成一个圈,然后编号从1~n,然后每个人有一个身高,然后要我们求出第i个人的左手边其第一个比他还高的人的编号,和右手边起第一个比他高的编号,然后如果这个人是最高,就直接输出-1.
分析:用单调栈,用dpl【i】来维护第 i 个人的左边的比他高的人的位置,然后dpr同理,然后由于是站一个圈,所以我们可以将这n个人扩展成3*n个人,然后通过n+1~2*n个人的dpl和dpr来得到答案。
代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<stack>
#include<math.h>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
typedef pair<int,int> pii;int a[100005*3];
int dpl[100005*3],dpr[100005*3];
stack<int>st; int main ()
{int T,n;scanf ("%d",&T);while (T--){scanf ("%d",&n);int M=-1;for (int i=1;i<=n;i++){scanf ("%d",&a[i]);a[n+i]=a[2*n+i]=a[i];M=max(M,a[i]);//求最大值 }dpl[1]=1;while (!st.empty())st.pop();st.push(1);for (int i=2;i<=3*n;i++){while (!st.empty()&&a[st.top()]<=a[i])st.pop();if (st.empty())dpl[i]=1;//如果栈已经为空 else {dpl[i]=st.top();}st.push(i);//进栈 }for (int i=n+1;i<=2*n;i++){if (a[i]==M)dpl[i]=-1;//如果是最高的,那就是-1 else if (dpl[i]>n)dpl[i]-=n;//否则判断是否大于n,是的话那就得-n }//      for (int i=1+n;i<=2*n;i++){//          printf ("%d ",dpl[i]);
//      }
//      printf ("\n");dpr[3*n]=3*n;while (!st.empty())st.pop();//类似上面 for (int i=3*n-1;i>=1;i--){while (!st.empty()&&a[st.top()]<=a[i])st.pop();if (st.empty()){dpr[i]=3*n;}else {dpr[i]=st.top();}st.push(i);}for (int i=n+1;i<=2*n;i++){if (a[i]==M)dpr[i]=-1;else if (dpr[i]>2*n)dpr[i]-=2*n;else if (dpr[i]>n)dpr[i]-=n;}//      for (int i=1+n;i<=2*n;i++){//          printf ("%d ",dpr[i]);
//      }
//      printf ("\n");for (int i=n+1;i<=2*n;i++){printf ("%d %d\n",dpl[i],dpr[i]);//答案 }}return 0;
}
/*
3
5
170 178 168 171 169
*/

文章标题 SPOJ - DRUIDEOI : Fata7y Ya Warda!(单调栈)相关推荐

  1. 让织梦内容页arclist标签的当前文章标题加亮显示

    很多人在用织梦做站的时候,会用到在当前栏目页面,给当前栏目标题使用指定样式如标题加亮,或者放个背景图.这是一个很常用和实用的功能,比如在导航页面,标识当前在浏览哪个栏目.如下图: 但是有些时候,我们在 ...

  2. dede首页调用文章标题|概述|所在栏目

    为什么80%的码农都做不了架构师?>>>    这里运用到的标签是{dede:arclist}{/dede:arclist},不需要修改数据库.快收藏吧 首页代码如下: <di ...

  3. 网络推广费用之你的文章标题优化“合格”了吗?

    网站内容在优化的过程中,文章的标题优化也是非常重要,一个好的标题能给网站优化带来很好的长尾关键词排名,带来更多的流量,那么对于网站文章标题该如何优化才能够吸引用户的关注呢?下面网络推广费用就带大家一起 ...

  4. 品牌网络推广方案浅析在编写文章标题时都有什么技巧?

    在做网站优化的时候,文章是相对于网站比较重要的方面,其中,文章页的标题优化也是有一定技巧的,才能让网站更好地发展,下面品牌网络推广方案就带大家一起来了解一如何给网站一个更好地标题. 1.文章标题取法 ...

  5. 外贸网络推广分享让网站文章标题优化效果更好地小技巧!

    在外贸网络推广网站优化过程中,对于文章优化来说,网站的标题优化是非常重要的,一个好的文章标题不仅能帮助网站吸引到更多的流量,还能给搜索引擎带来更多的流量, 那么外贸网络推广对于文章标题该如何撰写才能更 ...

  6. 产品网络推广浅析网站在优化时文章标题撰写要注意哪些事项?

    在网站优化中,产品网络推广很多优化新手们对文章编辑也有更多的了解,但想要做好更高质量的文章进行编辑也不简单,今天产品网络推广就带大家一起来了解一下网站优化时对于文章标题的撰写要注意哪些事项? 一.标题 ...

  7. 文章标题对网站优化有什么影响?

    众所周知,网站在进行优化的过程中,文章的内容对网站的优化有着非常重要的作用,网站排名靠前,一定离不开有高质量文章内容的支持.那么在文章内容的更新中,文章标题对网站优化来说都有哪些影响呢? 一.文章的标 ...

  8. 如何布局文章标题才更吸引搜索引擎注意?

    众所周知,搜索引擎能根据标题来确定页面的内容,然后对相关关键词进行排序,因此,标题不仅是网站的重要内容,也是文章内容的重要内容.那么我们该如何布局文章标题才能更吸引搜索引擎的注意呢? 一.关键字的标题 ...

  9. 写作心得——文章标题的重要性之一

    我不知道一些专业的作者们怎么看待文章的标题,但就以我自己的看法,我认为文章的标题是整篇文章的关键所在. 以前,在一些论坛中,见到网友们在谈论"标题党"的话题,并由此知道这是指一些以 ...

最新文章

  1. 微软私有云分享(R2)13 处理孤立资源
  2. Python--编码的疑惑
  3. 双层json报文样例_json报文解析
  4. 数据传输完整性_电缆监测数据传输系统分析与设计
  5. 码农即将被淘汰?未来10年,这样的程序员才值钱!
  6. 鼠绘漫画 for wp8.1
  7. 保证服务4个9的可用性的核心思路
  8. maya布料预设解析
  9. 2022泰迪杯B题思路解析(LSTM神经网络,时间序列ARIMA模型)可供学习参考
  10. 计算机专业考研好还是就业好?
  11. 高考0分作文精选--秋细雨PK叶闲花
  12. StyleGAN2发展介绍 花卉图像生成 模型修改
  13. taro+vue微信小程序文本换行
  14. DirectX 在MFC框架下的应用
  15. mysql编写1到n的奇数和_输入一整数n,输出1~n之间的奇数和(若n为奇数则包括n) 编程...
  16. 【Mybatis】Mybatis 注解开发
  17. 不支持发行版本5 解决方法
  18. 【秋招纪实录】一篇特别正经的【无领导小组讨论】经验分享
  19. python气象绘图相关链接
  20. Java下载Excel兼容性问题

热门文章

  1. 希利苏斯起始任务_希利苏斯战地任务快速完成方法 大地之击半天入手
  2. Python基础_Day_01_作业
  3. 海赢科技分享速卖通账号开店注册认证介绍及常见问题解答
  4. php 超级好用的特殊字符过滤函数
  5. 哎呦报错啦怎么办?nginx: [emerg] “server“ directive is not allowed here in /usr/local/nginx/conf/nginx.conf53
  6. From COM to COM 侯捷
  7. 一、零基础入门微信小程序开发之创建项目工程同时完成引导页开发
  8. linux修改SSH密码的方法
  9. 系统安全 --------- 账号安全管理
  10. python程序设计与算法基础江红答案_《Python程序设计与算法基础教程(第二版)》江红 余青松,第十一章课后习题答案...