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 ...
随机推荐
- 一个批量转换jtl文件的shell
最近在项目中遇到了批量转换jmeter测试结果jtl的问题,整了一个脚本,记录如下: #bin/sh filelist=`ls jtl` # 将jtl目录的所有文件列表读取并存入变量 for file ...
- 使用eclipse远程调试Tomcat的方法
tomcat是一种非常常见的java web应用服务器,有时候服务器可能并不是部署在本地,而是部署在远程其他的机器上,我们用eclispe该如何进行debug调试呢? 1. 在eclispe中新建we ...
- 简单Sql语句统计每年每个月的数据,每个月为数据的每列,简单SQL练习
有一张表,数据如下 请写出结果为以下的SQL语句. 在mysql中创建表 CREATE TABLE `aa` ( `id` int(10) NOT NULL AUTO_INCREMENT COMME ...
- Agri Net POJ1258 && Constructing Roads POJ2421
题意,在给出的图中,使用最小花费的边,使这个图仍然连通. #include <cstdio> #include <algorithm> #include <cstring ...
- 最短路径算法之四——SPFA算法
SPAF算法 求单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm,该算法是西南交通大学段凡丁于1994年发表的. 它可以在O(kE)的时间复杂度内求出源点 ...
- Android TextView结合SpannableString使用
super.onCreate(savedInstanceState); TextView txtInfo = new TextView(this); SpannableString ss = new ...
- How to Send an HTTP Header With Every Request With Spring RestTemplate
In Know Which Apps Are Hitting Your Web Service, I showed how to write a servlet filter that enforce ...
- 通过ngx-lua来统计Nginx上的虚拟主机性能数据
Web server调研分析 Filed under: Web Server — cmpan @ 2012-10-29 20:38:34 摘要 简单可依赖的架构首先需要有一个简单可依赖的前端WebSe ...
- BZOJ3155: Preprefix sum
题解: 写过树状数组搞区间修改和区间求和的就可以秒出吧... 代码: #include<cstdio> #include<cstdlib> #include<cmath& ...
- ehcache 分布式集群同步数据实例
本文使用rmi方式,借鉴百度能搜到的文章,但是均不能做到数据同步,做了些改动完全没问题,更详细说明介绍百度即可.直奔主题,可运行的demo实例! 创建一个maven项目,配置pom pom.xml & ...