zoukankan      html  css  js  c++  java
  • 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,转载请注明来源
  • 相关阅读:
    看从小自带BUFF的他,如何用代码降低万物互联的门槛
    30亿参数,华为云发布全球最大预训练模型,开启工业化AI开发新模式
    6大新品重磅发布,华为云全栈云原生技术能力持续创新升级
    ISO/IEC 5055:软件代码质量的标尺
    Python基础语法和数据类型最全总结
    40个问题让你快速掌握Java多线程的精髓
    编程实战:如何管理代码里的常量
    总是记不住java的IO流用法?用N个问题教你掌握java IO流
    4种语义分割数据集Cityscapes上SOTA方法总结
    PHP 在Swoole中使用双IoC容器实现无污染的依赖注入
  • 原文地址:https://www.cnblogs.com/zhber/p/7152890.html
Copyright © 2011-2022 走看看