• 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
    ",l[i+n]>n?l[i+n]-n:l[i+n],r[i]>n?r[i]-n:r[i]);
    50     }
    51 }
    Spoj DRUIDEOI
    ——by zhber,转载请注明来源
  • 相关阅读:
    restart xinetd
    Hello World——Linux汇编
    C++箴言:理解inline化的介入和排除
    网上找来的不错的Debian sources.list
    C/C++中static变量和static函数的用法
    Inline内联函数的总结贴
    C++箴言:理解inline化的介入和排除
    Hello World——Linux汇编
    网上找来的不错的Debian sources.list
    C/C++中static变量和static函数的用法
  • 原文地址:https://www.cnblogs.com/zhber/p/7152890.html
Copyright © 2020-2023  润新知