poj 3250 Bad Hair Day【栈】
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 15922 | Accepted: 5374 |
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
题意:每头牛都有身高,他(a)前边站的也有牛,有比他高的有比他低的,他能看见他前边比他低的,但是当有比他高的牛(b)的时候,这个高牛b前边的比a牛低的 a牛就看不见了(符合日常生活,当前边有一个比自己高的牛时,自己的视线就被挡住了)
题解:每次让牛入栈,当入栈的牛比栈顶的牛低时,继续入栈,直到遇到比栈顶高的牛弹出栈顶,
#include<stdio.h>
#include<stack>
#include<iostream>
#include<string.h>
using namespace std;
stack<int>s;
int cow[80005];
int main()
{
int n,i;
int sum=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&cow[i]);
s.push(cow[1]);
for(i=2;i<=n;i++)
{
while(!s.empty()&&s.top()<=cow[i])
s.pop();
sum+=s.size();
s.push(cow[i]);
}
printf("%u\n",sum);
return 0;
}
poj 3250 Bad Hair Day【栈】的更多相关文章
- Poj 3250 单调栈
1.Poj 3250 Bad Hair Day 2.链接:http://poj.org/problem?id=3250 3.总结:单调栈 题意:n头牛,当i>j,j在i的右边并且i与j之间的所 ...
- poj 3250 Bad Hair Day (单调栈)
http://poj.org/problem?id=3250 Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- poj 3250 Bad Hair Day(栈的运用)
http://poj.org/problem?id=3250 Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- POJ 3250 Bad Hair Day(单调栈)
[题目链接] http://poj.org/problem?id=3250 [题目大意] 有n头牛,每头牛都有一定的高度,他能看到在离他最近的比他高的牛前面的所有牛 现在每头牛往右看,问每头牛能看到的 ...
- POJ 3250 Bad Hair Day --单调栈(单调队列?)
维护一个单调栈,保持从大到小的顺序,每次加入一个元素都将其推到尽可能栈底,知道碰到一个比他大的,然后res+=tail,说明这个cow的头可以被前面tail个cow看到.如果中间出现一个超级高的,自然 ...
- poj 3250 Bad Hair Day 单调栈入门
Bad Hair Day 题意:给n(n <= 800,000)头牛,每头牛都有一个高度h,每头牛都只能看到右边比它矮的牛的头发,将每头牛看到的牛的头发加起来为多少? 思路:每头要进栈的牛,将栈 ...
- poj 3250 Bad Hair Day (单调栈)
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14883 Accepted: 4940 Des ...
- Bad Hair Day POJ - 3250 (单调栈入门题)
Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-cons ...
- POJ 3250 Bad Hair Day【单调栈入门】
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24112 Accepted: 8208 Des ...
随机推荐
- spring结合时,web.xml的配置
<!-- 1. web.xml配置 <context-param> <param-name>webAppRootKey</param-name> <pa ...
- 如何:在 Winform 动态启动、控制台命令行?
需求 winForm 程序输出类型为 windows 程序(不是命令行程序) 在运行时想输入一些信息编译开发调试,如何实现这一功能 解答: AllocConsole.FreeConsole ...
- [转]vim ruby等的ide设置
使用vim做rails开发,推荐这个 https://github.com/carlhuda/janus 1. vim下的Rails常用插件 首先列出我比较常用的vim插件,基本都是网上提到的哪些.必 ...
- nodejs创建express+ejs项目
很多同学跟着 node.js开发指南 学习用 express+ejs 创建项目的时候,却总是无法成功,如: 运行 以下命令去创建 express+ejs 创建项目 $ express -t ejs m ...
- 深入理解Java虚拟机 - 垃圾收集算法与垃圾收集器
1. 垃圾收集算法 JVM的垃圾收集算法在不同的JVM实现中有所不同,且在平时工作中一般不会深入到收集算法,因此只对算法做较为简单的介绍. 1.1 标记-清除算法 ...
- WordPress 3.8.1 /xmlrpc.php拒绝服务漏洞
漏洞版本: WordPress 3.8.1 漏洞描述: WordPress是一款内容管理系统. WordPress 3.8.1 /xmlrpc.php 文件有ping其他主机的功能,通过这个功能可以请 ...
- 自定义View 实现软键盘实现搜索
1. xml文件中加入自定义 搜索view <com.etoury.etoury.ui.view.IconCenterEditText android:id="@+id/search_ ...
- 如何从Linux源码获知版本信息
/*************************************************************************** * 如何从Linux源码获知版本信息 * 声明 ...
- 【原创】 Shuffling
在机器学习领域中,经常会听到“shuffling"这个术语.那么,shuffling到底是什么意思呢. 通常,shuffling指的是在SGD怎样依赖训练数据输入顺序的算法中,将训练数据随机 ...
- ORACLE参数文件
一.oracle pfile/spfile区别 pfile 默认的名称为“init+例程名.ora”文件路径:E:\oracle\product\10.2.0\db_1\dbs,这是一个文本文件 ...