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 << "\n";
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

 

posted @ 2015-06-06 12:14  JSZX11556  阅读(210)  评论(0编辑  收藏  举报