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,每头牛都只能看到右边比它矮的牛的头发,将每头牛看到的牛的头发加起来为多少? 思路:每头要进栈的牛,将栈 ...
随机推荐
- Python学习——numpy.random
numpy.random.rand numpy.random模块作用是生成随机数,其中numpy.random.rand(d0, d1, ..., dn):生成一个[0,1)之间的随机浮点数或N维浮点 ...
- 嵌入式linux:通过qemu模拟mini2440开发环境
1 编译安装QEMU 首先下载qemu for mini2440,直接打包下载 http://repo.or.cz/w/qemu/mini2440.git/snapshot/HEAD.tar.gz ...
- LeetCode954二倍数对数组
问题:二倍数对数组 给定一个长度为偶数的整数数组 A,只有对 A 进行重组后可以满足 “对于每个 0 <= i < len(A) / 2,都有 A[2 * i + 1] = 2 * A[2 ...
- 【转】iPhone通讯录AddressBook.framework和AddressBookUI.framework的应用
通讯录中联系人相关的应用iPhone提供了两个框架:AddressBook.framework和AddressBookUI.framework,使用这两个框架我们可以在程序中访问并显示iPhone数据 ...
- usb gadge驱动设计之我是zero
此处将以zero.c为例进行讲解. 第一次接触zero.c驱动的时候,是因为某项目需要,提供一种usb字符设备,希望能够通过字符设备打开,读取和发送文件.当时能想到的就是zero.c文件,本打算按照z ...
- Codeforces Round #464 (Div. 2) B. Hamster Farm
B. Hamster Farm time limit per test2 seconds memory limit per test256 megabytes Problem Description ...
- kettle - Linux下定时执行kettle作业
Linux下实现kettle 自动同步数据 1.安装jdk tar -zxvf jdk-7u25-linux-x64.tar.gz -C /usr/share 2.配置java环境 vim /etc/ ...
- 《鸟哥的Linux私房菜》学习笔记(0)——磁盘与文件系统管理
一.Linux的登陆流程 login: 用户名:每个用户名都有一个用户ID(用户标识符),计算机处理的就是用户ID(数字)而不是用户名(字符),. 认证机制:Authentication,比如密码或者 ...
- HDU 5657 CA Loves Math 状压DP + 枚举
题意: 给出\(A(2 \leq A \leq 11), n(0 \leq n \leq 10^9), k(1 \leq k \leq 10^9)\). 求区间\([1, A^n]\)中各个数字互不相 ...
- laravel5.2总结--门面(facades)
Facades 为应用程序的服务容器中可用的类提供了一个「静态」接口. Laravel 本身附带许多的 facades,甚至你可能在不知情的状况下已经在使用他们! xpower的静态接口(门面 ...