• BZOJ 3401: [Usaco2009 Mar]Look Up 仰望( 单调栈 )


    n <= 105 , 其实是10 ^ 5 ....坑...我一开始写了个模拟结果就 RE 了.. 发现这个后写了个单调栈就 A 了...

    ---------------------------------------------------------------------------------------------

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<stack>
     
    #define rep( i , n ) for( int i = 0 ; i < n ; ++i )
    #define clr( x , c ) memset( x , c , sizeof( x ) )
    #define Rep( i , n ) for( int i = 1 ; i <= n ; ++i )
     
    using namespace std;
     
    const int maxn = 100000 + 5;
     
    struct node {
    int x , h;
    };
     
    int h[ maxn ];
    int ans[ maxn ];
    stack< node > S;
     
    int main() {
    freopen( "test.in" , "r" , stdin );
    int n;
    cin >> n;
    rep( i , n ) scanf( "%d" , &h[ i ] );
    clr( ans , 0 );
    rep( i , n ) {
    while( ! S.empty() && S.top().h < h[ i ] ) {
    node o = S.top();
    S.pop();
    ans[ o.x ] = i + 1;
    }
    S.push( ( node ) { i , h[ i ] } );
    }
    rep( i , n ) 
       printf( "%d " , ans[ i ] );
    return 0;
    }

    ---------------------------------------------------------------------------------------------

    3401: [Usaco2009 Mar]Look Up 仰望

    Time Limit: 3 Sec  Memory Limit: 128 MB
    Submit: 142  Solved: 85
    [Submit][Status][Discuss]

    Description

    约翰的N(1≤N≤105)头奶牛站成一排,奶牛i的身高是Hi(l≤Hi≤1,000,000).现在,每只奶牛都在向左看齐.对于奶牛i,如果奶牛j满足i<j且Hi<Hj,我们可以说奶牛i可以仰望奶牛j.    求出每只奶牛离她最近的仰望对象.

    Input

     
        第1行输入N,之后每行输入一个身高.

    Output

     
        共N行,按顺序每行输出一只奶牛的最近仰望对象.如果没有仰望对象,输出0.

    Sample Input

    6
    3
    2
    6
    1
    1
    2

    Sample Output

    3
    3
    0
    6
    6
    0

    HINT

    Source

  • 相关阅读:
    测试驱动开发的意义何在
    Web自动化测试模式page object的小利器:gizmo
    在NANT使用Nunit2标签运行Nunit测试
    小试牛刀 Ruby on Rails
    敏捷回顾会议的思考
    ThoughtWorks技术校园行第二波 课程资料 CleanCode&DirtyCode
    从git merge 和 git rebase想到……
    Ruby中的深浅拷贝
    NUnit Extension小介绍
    如何写好的测试呢?
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/4581937.html
Copyright © 2020-2023  润新知