junior19

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

DRUIDEOI - Fata7y Ya Warda!

no tags 

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 (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

Note

The Third sample.

题意:n个数形成一个环,1号的左边是n号,位置i的数值是ai,输出每一个数左边和右边,第一个比他大的数的位置,如果不存在这样的位置,输出-1。

思路:因为是一个圆圈,所以将数组开大两倍,模拟转圈,然后维护一个单调递增栈。

mycode:

# include <stdio.h>
# include <string.h>
# include <stack>
# define MAXN 100000
using 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]);
				else
					printf("%d", v[i - n] - n);
			printf("\n");
		}
	}
			
}



posted on 2017-01-25 21:29  junior19  阅读(142)  评论(0编辑  收藏  举报