博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BZOJ 1660: [Usaco2006 Nov]Bad Hair Day 乱发节( 单调栈 )
阅读量:4311 次
发布时间:2019-06-06

本文共 1161 字,大约阅读时间需要 3 分钟。

 维护一个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
[ ][ ][ ]

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

 

转载于:https://www.cnblogs.com/JSZX11556/p/4556443.html

你可能感兴趣的文章
mcast_set_if函数
查看>>
C-RAN 集中化、协作化、云化、绿色节能(4C)
查看>>
统计抽样
查看>>
链表(2)
查看>>
实验四
查看>>
P1341 无序字母对(欧拉回路)
查看>>
UOJ #349. 【WC2018】即时战略
查看>>
15天玩转redis —— 第十篇 对快照模式的深入分析
查看>>
理解MapReduce计算构架
查看>>
【BZOJ 3473】 字符串 (后缀数组+RMQ+二分 | 广义SAM)
查看>>
jQuery渐隐渐出的文字提示
查看>>
异常记录处理
查看>>
如何定位到append的当前位置,不用拉滚动条scrollIntoView方法
查看>>
我的第一篇Window Live Writer日志
查看>>
MySQL编码、Spring配置中编码、Struts中文问题
查看>>
Controller中使用过滤器
查看>>
Anaconda+django写出第一个web app(八)
查看>>
模拟 HDOJ 5099 Comparison of Android versions
查看>>
关于http的post传送文件
查看>>
eclipse 快速导入所有需要的包
查看>>