spoj:DRUIDEOI - Fata7y Ya Warda!(单调栈)
来源:互联网 发布:收购淘宝买家订单数据 编辑:程序博客网 时间:2024/05/17 01:02
DRUIDEOI - 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:
35
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
Note
题意:n个数形成一个环,1号的左边是n号,位置i的数值是ai,输出每一个数左边和右边,第一个比他大的数的位置,如果不存在这样的位置,输出-1。
思路:因为是一个圆圈,所以将数组开大两倍,模拟转圈,然后维护一个单调递增栈。
mycode:
# include <stdio.h># include <string.h># include <stack># define MAXN 100000using namespace std;int a[MAXN<<1+3], l[MAXN<<1+3], r[MAXN<<1+3], t, n, i, j;int main(){ stack<int>st; scanf("%d",&t); while(t--) { memset(l, 0, sizeof(l)); memset(r, 0, sizeof(r)); while(!st.empty()) st.pop(); scanf("%d",&n); for(i=1; i<=n; ++i) scanf("%d",&a[i]), a[i+n] = a[i]; for(i=1; i<=(n<<1); ++i)//计算左边 { if(st.empty()) { l[i] = -1; st.push(i); } else if(a[i] >= a[st.top()]) { while(!st.empty() && a[i]>=a[st.top()]) st.pop(); if(!st.empty() && st.top()!=i-n) l[i] = st.top(); else l[i] = -1; st.push(i); } else { l[i] = st.top(); st.push(i); } } while(!st.empty()) st.pop(); for(i=n<<1; i>=1; --i)//计算右边 { if(st.empty()) { r[i] = -1; st.push(i); } else if(a[i] >= a[st.top()]) { while(!st.empty() && a[i]>=a[st.top()]) st.pop(); if(!st.empty() && st.top()!=i+n) r[i] = st.top(); else r[i] = -1; st.push(i); } else { r[i] = st.top(); st.push(i); } } for(i=n+1; i<=(n<<1); ++i)//输出 { if(l[i]<=n) printf("%d ",l[i]); else printf("%d ",l[i]-n); if(r[i-n]<=n) printf("%d\n",r[i-n]); else printf("%d\n",r[i-n]-n); } } return 0;}
others code:from Walker
#include <cstdio>#include <cstdlib>#include <cstring>#include <ctime>#include <cmath>#include <iostream>#include <algorithm>using namespace std;int test, n, a[200001], f[200001], v[200001]; int main() {//freopen("1.txt", "r", stdin);//freopen("2.txt", "w", stdout);scanf("%d", &test);for (; test--; ) {scanf("%d", &n);for (int i = 1; i <= n; i++)scanf("%d", &a[i]), a[i + n] = a[i];for (int i = 1; i <= n << 1; i++) {int now = i - 1;for (; now && a[i] >= a[now]; now = f[now]);f[i] = now;}for (int i = n << 1; i; --i) {int now = i + 1;for (; now != (n << 1) + 1 && a[i] >= a[now]; now = v[now]);v[i] = now;}for (int i = n + 1; i <= n << 1; i++){if (!f[i]) printf("-1");else if (f[i] <= n)printf("%d", f[i]);else printf("%d", f[i] - n);printf(" ");if (v[i - n] == n + n + 1) printf("-1");else if (v[i - n] <= n)printf("%d", v[i - n]);elseprintf("%d", v[i - n] - n);printf("\n");}}}
- spoj:DRUIDEOI - Fata7y Ya Warda!(单调栈)
- SPOJ Fata7y Ya Warda! 裸单调栈 1月25日
- SPOJ DRUIDEOI (单调栈)
- SPOJ DRUIDEOI 优先队列
- SPOJ 24258 DRUIDEOI
- spoj-3415(后缀数组+单调栈)
- DRUIDEOI
- 二分+单调栈 SPOJ MINSUB
- SPOJ MINSUB 二分+单调栈
- SPOJ MINSUN Largest Submatrix [二分+单调栈]
- [SPOJ MINSUB Largest Submatrix]单调栈
- SPOJ 912 MATRIX2(二维单调队列)
- SPOJ-MINSUB - Largest Submatrix(单调栈,二分,好题)
- 【SPOJ-ARRAYSUB】subarrays【单调队列】
- poj3250(单调栈)
- FZU2190(单调栈)
- 单调栈(山峰)
- 山峰(单调栈)
- 论学历与出身
- UVa1586 分子量 算法竞赛入门经典第二版CH-3习题3-2
- Maven学习总结:几个常用的maven插件
- 基于数组的队列操作
- Init-param和Context-param的区别
- spoj:DRUIDEOI - Fata7y Ya Warda!(单调栈)
- tomcat之servlet容器
- BZOJ 4443 [Scoi2015]小凸玩矩阵
- 线性表知识点小结
- Spring Boot Web应用开发 CORS 跨域请求支持
- 图片木马制作的三种方法
- 小目标_移植FreeRTOS到STM32F412
- caffe问题汇总
- Balanced Binary Tree