FZU 1845 Look Up(单调栈)

来源:互联网 发布:收费站车流量数据分析 编辑:程序博客网 时间:2024/04/30 04:23

Description

N (1 <= N <= 100,000) monkeys in the mountains, conveniently numbered 1..N, are once again standing in a row. Monkey i has height Hi (1 <= Hi <= 1,000,000).

Each monkey is looking to his left toward those with higher index numbers. We say that monkey i "looks up" to monkey j if i < j and Hi < Hj. For each monkey i, we would like to know the index of the first monkey in line looked up to by monkey i.

Input

Input consists of several testcases. The format of each case as follow:

  • Line 1: A single integer: N
  • Lines 2..N+1: Line i+1 contains the single integer: Hi

  • Output

    For each testcase, output N lines. Line i contains a single integer representing the smallest index of a monkey up to which monkey i looks. If no such monkey exists, print 0.

    Sample Input

    6326112

    Sample Output

    330660

    题意:问你一个猴子向左看最远能看多远。

    分析:搞一个单调递增栈就行了。

    #include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int INF=0x3f3f3f3f;typedef long long LL;const int maxn=1e5+100;int s[maxn],p[maxn];int a[maxn],n,R[maxn];int main(){    while(~scanf("%d",&n))    {        REPF(i,1,n)          scanf("%d",&a[i]);        int top=0;        for(int i=n;i>=1;i--)        {            while(top>0&&a[i]>=a[s[top]])               --top;            R[i]=s[top];            s[++top]=i;        }        for(int i=1;i<=n;i++)            printf("%d\n",R[i]);    }}


    0 0
    原创粉丝点击