Spoj-DRUIDEOI Fata7y Ya Warda!
Posted zhber
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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在这里)
找到每个点左右距离最近的,严格比它矮的第一个人编号
然后就是左右单调递减栈搞一搞,因为是个环所以要长度复制一倍
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<deque> 9 #include<set> 10 #include<map> 11 #include<ctime> 12 #define LL long long 13 #define inf 0x7ffffff 14 #define pa pair<int,int> 15 #define mkp(a,b) make_pair(a,b) 16 #define pi 3.1415926535897932384626433832795028841971 17 using namespace std; 18 inline LL read() 19 { 20 LL x=0,f=1;char ch=getchar(); 21 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 22 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 23 return x*f; 24 } 25 int n,top; 26 int zhan[300010]; 27 int a[300010]; 28 int l[300010],r[300010]; 29 int main() 30 { 31 int T=read(); 32 while (T--) 33 { 34 n=read();for (int i=1;i<=n;i++)a[i]=a[i+n]=a[i+2*n]=read(); 35 top=0; 36 for (int i=1;i<=2*n;i++) 37 { 38 while (top&&a[zhan[top]]<=a[i])top--; 39 l[i]=top?zhan[top]:-1; 40 zhan[++top]=i; 41 } 42 top=0; 43 for (int i=2*n;i>=1;i--) 44 { 45 while (top&&a[zhan[top]]<=a[i])top--; 46 r[i]=top?zhan[top]:-1; 47 zhan[++top]=i; 48 } 49 for (int i=1;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]); 50 } 51 }
以上是关于Spoj-DRUIDEOI Fata7y Ya Warda!的主要内容,如果未能解决你的问题,请参考以下文章
TM32MP1核心板资料(基于米尔电子MYC-YA157C)
python Tornado'ya Markdown takviyesi