Bad Hair Day_单调栈
Description
Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows' heads.
Each cow i has a specified height hi (1 ≤ hi ≤ 1,000,000,000) and is standing in a line of cows all facing east (to the right in our diagrams). Therefore, cow i can see the tops of the heads of cows in front of her (namely cows i+1, i+2, and so on), for as long as these cows are strictly shorter than cow i.
Consider this example:
=
= =
= - = Cows facing right -->
= = =
= - = = =
= = = = = =
1 2 3 4 5 6
Cow#1 can see the hairstyle of cows #2, 3, 4
Cow#2 can see no cow's hairstyle
Cow#3 can see the hairstyle of cow #4
Cow#4 can see no cow's hairstyle
Cow#5 can see the hairstyle of cow 6
Cow#6 can see no cows at all!
Let ci denote the number of cows whose hairstyle is visible from cow i; please compute the sum of c1 through cN.For this example, the desired is answer 3 + 0 + 1 + 0 + 1 + 0 = 5.
Input
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i.
Output
Sample Input
6
10
3
7
4
12
2
Sample Output
5
【题意】给出一些牛的高度,站成一排,往右看,求所有牛能看见的牛的数量之和。
【思路】单调栈。从左到右取当前牛的高度,从栈顶开始把高度小于或等于当前牛的高度的那些元素删除,此时栈中剩下的元素的数量就是可以看见当前牛的其他牛的数量。把这个数量加在一起,即答案。
#include<iostream>
#include<stdio.h>
#include<stack>
#include<string.h>
using namespace std;
int main()
{
stack<int>st;
long long int sum;
int n,h;
while(~scanf("%d",&n))
{
while(!st.empty())
{
st.pop();
}
sum=;
scanf("%d",&h);
st.push(h);
for(int i=;i<n;i++)
{
scanf("%d",&h);
while(!st.empty()&&h>=st.top())
st.pop();
sum+=st.size();
st.push(h);
}
printf("%lld\n",sum); }
return ;
}
Bad Hair Day_单调栈的更多相关文章
- [bzoj1660][Usaco2006 Nov]Bad Hair Day_单调栈
Bad Hair Day bzoj-1660 Usaco-2006 Nov 题目大意:n头牛站成一列,每头牛向后看.f[i]表示第i头牛到第n头牛之间有多少牛,使得这些牛都比i矮,且中间没有比i高的牛 ...
- BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 8748 Solved: 3835[Submi ...
- BZOJ 4453: cys就是要拿英魂![后缀数组 ST表 单调栈类似物]
4453: cys就是要拿英魂! Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 90 Solved: 46[Submit][Status][Discu ...
- BZOJ 3238: [Ahoi2013]差异 [后缀数组 单调栈]
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2326 Solved: 1054[Submit][Status ...
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
- bzoj1510: [POI2006]Kra-The Disks(单调栈)
这道题可以O(n)解决,用二分还更慢一点 维护一个单调栈,模拟掉盘子的过程就行了 #include<stdio.h> #include<string.h> #include&l ...
- BZOJ1057[ZJOI2007]棋盘制作 [单调栈]
题目描述 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴阳. 而我们的 ...
- 洛谷U4859matrix[单调栈]
题目描述 给一个元素均为正整数的矩阵,上升矩阵的定义为矩阵中每行.每列都是严格递增的. 求给定矩阵中上升子矩阵的数量. 输入输出格式 输入格式: 第一行两个正整数n.m,表示矩阵的行数.列数. 接下来 ...
- POJ3250[USACO2006Nov]Bad Hair Day[单调栈]
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17774 Accepted: 6000 Des ...
随机推荐
- javaSE基础——常见的dos命令即其他
常用的DOS命令 dir(directory) : 列出当前目录下的文件以及文件夹 md(make directory) : 创建目录 rd(remove directory) : 删除目录 ...
- ARM 汇编的一些规范
A.5.1 文件格式 ARM 源程序文件(即源文件)为文件格式,可以使用任一文本编辑器编写程序代码. 在一个项目中,至少要有一个汇编源文件或C 程序文件,可以有多个汇编 ...
- Android开发--Activity的创建
1.Activity概述 Activity是android四大基本组件之一.每一个activity文件对应一个界面,一个程序由多个activity组成. 2.Android工作目录
- Java多线程-新特征-锁(下)
在上文中提到了Lock接口以及对象,使用它,很优雅的控制了竞争资源的安全访问,但是这种锁不区分读写,称这种锁为普通锁.为了提高性能,Java提供了读写锁,在读的地方使用读锁,在写的地方使用写锁,灵活控 ...
- Akumuli时间序列数据库——列存储,LSM,MVCC
Features Column-oriented time-series database. Log-structured append-only B+tree with multiversion c ...
- [转]Web开发的发展史
之所以转这篇文章是因为它可以对web请求有大致的理解. 以下内容转自:http://www.open-open.com/news/view/19ed96a 英文原文: Full stack web d ...
- Ubuntu 设置su密码
如果之前安装时没有设置root密码,可以如下设置: 命令窗口中输入:sudo passwd [sudo] password for 用户名: 这里输入你sudo 的密码输入新的 UNIX 密码: 重 ...
- js基础之动画(一)
一.让div动起来 var oBtn = document.getElementById('btn1'); var timer='';//设置定时器 oBtn.onclick=function st ...
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- 启用jboss热部署
Please make sure to add <configuration> <jsp-configuration deve ...