zoukankan      html  css  js  c++  java
  • BZOJ 1660: [Usaco2006 Nov]Bad Hair Day 乱发节( 单调栈 )

     维护一个h严格递减的栈 , 出栈时计算一下就好了..

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

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<stack>
    #include<iostream>
     
    #define rep( i , n ) for( int i = 0 ;  i < n ; ++i )
    #define clr( x , c ) memset( x , c , sizeof( x ) )
     
    using namespace std;
     
    stack< pair< int , int > > S;
     
    int main() {
    freopen( "test.in" , "r" , stdin );
    int n;
    cin >> n;
    long long ans = 0;
    int t;
    for( int i = 0 ; i <= n ; ++i ) {
    if( i != n )
       scanf( "%d" , &t );
    else 
       t = int( 2e9 );
    while( ! S.empty()  && S.top().second <= t ) {
    ans += i - S.top().first;
    S.pop();
    }
    S.push( make_pair( i + 1 , t ) );
    }
    cout << ans << " ";
    return 0;
    }

      

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

    1660: [Usaco2006 Nov]Bad Hair Day 乱发节

    Time Limit: 2 Sec  Memory Limit: 64 MB
    Submit: 716  Solved: 337
    [Submit][Status][Discuss]

    Description

    Input

    * Line 1: 牛的数量 N。

     * Lines 2..N+1: 第 i+1 是一个整数,表示第i头牛的高度。

    Output

    * Line 1: 一个整数表示c[1] 至 c[N]的和。

    Sample Input

    6
    10
    3
    7
    4
    12
    2


    输入解释:

    六头牛排成一排,高度依次是 10, 3, 7, 4, 12, 2。

    Sample Output

    5

    3+0+1+0+1=5

    HINT

    Source

  • 相关阅读:
    汉诺塔
    美丽联合2018前端
    游览器
    python多线程实现
    cuda toolkit
    编译器,解释器及混合编译
    大疆无人机
    SaaS PaaS IaaS mes
    CNN中权值共享的理解
    最近动作项目心得
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/4556443.html
Copyright © 2011-2022 走看看