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

  • 相关阅读:
    SpringBoot定制修改Servlet容器
    springboot配置i18n
    idea properties编码问题
    多线程分页查询
    浏览器 私有属性&内核
    css添加样式的四种方式
    html、css、js注释
    JS中的 变量提升
    ECMAScript
    NOSCRIPT标签的用处
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/4556443.html
Copyright © 2011-2022 走看看