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 ...
随机推荐
- 2013 Multi-University Training Contest 5 Partition
思路:五边形数定理!!! 五边形数定理是一个由欧拉发现的数学定理,描述欧拉函数展开式的特性.欧拉函数的展开式如下: 亦即 欧拉函数展开后,有些次方项被消去,只留下次方项为1, 2, 5, 7, 12, ...
- 2013 Multi-University Training Contest 5 k-th point
刚开始我也不知道怎么做,后来慢慢就推出来了…… 对于样例 2 1 0,结果是2/3 2 2 0,结果是4/5 3 2 0,结果是6/7 3 2 1,结果是9/14=6/7*3/4 …… 之后就会发现每 ...
- highChartTable 切换
<!doctype html> <html lang="en"> <head> <script type="text/javas ...
- nginx几个知识点汇总
WHY? 为什么用Nginx而不用LVS? 7点理由足以说明一切:1 .高并发连接: 官方测试能够支撑 5 万并发连接,在实际生产环境中跑到 2 - 3 万并发连接数.?2 .内存消耗少: 在 3 万 ...
- 使用typeid(变量或类型).name()来获取常量或变量的类型---gyy整理
使用typeid(变量或类型).name()来获取常量或变量的类型 <typeinfo> 该头文件包含运行时类型识别(在执行时确定数据类型)的类 typeid的使用 typeid操作 ...
- IOS - IOS之同步请求、异步请求、GET请求、POST请求
1.同步请求可以从因特网请求数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据完成,才可以进行下一步操作, 2.异步请求不会阻塞主线程,而会建立一个新的线程来操作,用户发出异步请求后,依然 ...
- JS中的自执行函数
本来规划的是2013年,狠狠的将JS学习下,谁知计划赶不上变化,计划泡汤了.13年的我对JS来说可以说是属于跟风,对它的理解和认识也仅仅是皮毛而已,也是因为要完成<ArcGIS API for ...
- Nginx+uWSGI或fastcgi部署Django项目
nginx+uWSGI ubuntu下先安装下C编译器和Python环境: sudo apt-get install build-essential python-dev 使用pip安装uWSGI: ...
- Windows下Vim设置
进入安装目录,找到_vimrc文件,用文本编辑器打开,在前面加入下述内容 设置中文支持 " 设置编码自动识别, 中文引号显示 " set fileencodings=utf-8,c ...
- grep 同时满足多个关键字和满足任意关键字
grep 同时满足多个关键字和满足任意关键字 ① grep -E "word1|word2|word3" file.txt 满足任意条件(word1.word2和word ...