poj 3250 Bad Hair Day
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 21084 | Accepted: 7202 |
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 题意:个子高的牛能看到个子低的牛,且每头牛的视线都是一个方向,问每头牛分别能看到视线方向上的多少头牛。
思路:单调栈,从后往前考虑队列,若当前的牛能看到存储在栈中的牛,则把栈中的牛从栈中抛出,直到栈为空或者栈中的牛的身高大于等于当前牛的身高为止,将当前的牛压入栈中,每次执行这样的操作即可。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
#define INF 0x3f3f3f3f
#define N_MAX 80000+20
typedef long long ll;
int n,h[N_MAX];
int st[N_MAX],num[N_MAX];
int main() {
scanf("%d",&n);
for (int i = ; i < n; i++)scanf("%d",&h[i]);
int t = ;//栈的大小
for (int i = n-; i >=; i--) {
while (t > && h[st[t - ]] <h[i])t--;
num[i] = t == ? n - - i : st[t - ] - i - ;
st[t++]= i;
}
ll sum = ;
for (int i = ; i < n; i++)
sum += num[i];
printf("%lld\n",sum);
return ;
}
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 -- POJ -- 3250
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 栈应用
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #d ...
- poj 3250 Bad Hair Day【栈】
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15922 Accepted: 5374 Des ...
- poj 3250 Bad Hair Day 单调栈入门
Bad Hair Day 题意:给n(n <= 800,000)头牛,每头牛都有一个高度h,每头牛都只能看到右边比它矮的牛的头发,将每头牛看到的牛的头发加起来为多少? 思路:每头要进栈的牛,将栈 ...
随机推荐
- 【转】【win网络编程】socket中的recv阻塞和select的用法
在编写ftp客户端程序时,在联通后使用recv函数进行接收欢迎信息时,需要申请内存进行接收数据保存,一次读取成功,但是由于一个随机的ftp服务端在说,欢迎信息的大小是不知道的,所以在尝试使用死循环,在 ...
- 项目十八-Hadoop+Hbase分布式集群架构“完全篇”
本文收录在Linux运维企业架构实战系列 前言:本篇博客是博主踩过无数坑,反复查阅资料,一步步搭建,操作完成后整理的个人心得,分享给大家~~~ 1.认识Hadoop和Hbase 1.1 hadoop简 ...
- CentOS Linux 安装IPSec+L2TP
第二层隧道协议L2TP(Layer 2 Tunneling Protocol)是一种工业标准的Internet隧道协议,它使用UDP的1701端口进行通信.L2TP本身并没有任何加密,但是我们可以使用 ...
- 递推:Number Sequence(mod找规律)
解题心得: 1.对于数据很大,很可怕,不可能用常规手段算出最后的值在进行mod的时候,可以思考找规律. 2.找规律时不必用手算(我傻,用手算了好久).直接先找前100项进行mod打一个表出来,直接看就 ...
- Java并发模型框架
构建Java并发模型框架 Java的多线程特性为构建高性能的应用提供了极大的方便,但是也带来了不少的麻烦.线程间同步.数据一致性等烦琐的问题需要细心的考虑,一不小心就会出现一些微妙的,难以调试的错误. ...
- 10 RESTful 实现权限控制
1 2 身份控制 3 4 只能显示5条数据 5 权限控制 序列化渲染深度 6
- P3376 【模板】网络最大流dinic算法
P3376 [模板]网络最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点 ...
- 谋哥:《App自推广》连载2直立人行走迁徙
[谋哥每天一干货,第六十九篇] 前篇说到声音在远古时代,是一个神奇的东西,它能够很快地把信息传播到其他地方,突破了短距离.然而能人的后代直立人学会了直立行走,他们开始走出非洲,到达遥远的中东.中国,还 ...
- Windows核心编程小结1
这本书绝对经典,看看定会增加不少知识.当然这本书有很多东西比<Windows程序设计第五版>中的更加详细. 1.Unicode:宽字节字符集 这是一个国际的字符标准,16位,最大可支持65 ...
- C# 序列化和反序列化 详解
什么是序列化以及如何实现序列化? 如何将对象数据写入 XML 文件? 如何从 XML 文件读取对象数据? 什么是序列化以及如何实现序列化? 序列化是通过将对象转换为字节流,从而存储对象或将对象传输到内 ...