zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    常用的系统存储过程
    考勤信息(员工打卡)
    项目经理评分(评价)
    体验套餐管理系统
    C#中简单的继承和多态
    面向对象的七个设计原则
    office 2010 安装时出错 MSXML版本6.10.1129.0
    phpstorm + Xdebug断点调试PHP
    wamp server 3.0.0 修改默认浏览器,软件语言和配置文件编辑器
    vue.js指令v-model实现方法
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/4581937.html
Copyright © 2011-2022 走看看