poj3250 Bad Hair Day
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
一些牛从左到右排列。全部的牛都从左往右看,左边的牛仅仅能看到右边的比它身高严格小的牛的发型。假设被一个大于等于它身高的牛挡住。那么它就不能看到再右边的牛。要求每头牛能够看到其它牛的总数。转化一下,事实上就是求每头牛被看到的总次数。能够用单调栈,每次删除栈中比当前牛的身高小于等于的数。事实上这题也能够看做是单调队列。但由于不用对对首操作。所以可看做退化为了栈。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 80600
int a[maxn],stack[maxn];
int main()
{
int n,m,i,j,top;
__int64 sum;
while(scanf("%d",&n)!=EOF){
memset(stack,0,sizeof(stack));
top=0;sum=0;
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
while(top>0 && a[i]>=stack[top])top--;
sum+=top;
stack[++top]=a[i];
}
printf("%I64d\n",sum);
}
return 0;
}
poj3250 Bad Hair Day的更多相关文章
- poj3250
//(栈)poj3250将第i头牛能看到多少牛传化为第i头牛能被多少牛看见 /* #include <stdio.h> #include <stack> using names ...
- POJ3250[USACO2006Nov]Bad Hair Day[单调栈]
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17774 Accepted: 6000 Des ...
- bzoj1057,poj3250
bzoj1057本质上是求最大子矩阵: 第一问是一个经典的O(n2)dp 第二问就是最大子矩阵,回眸一下当年卡了我很久的问题: 首先穷举显然不行(这不废话吗?): 首先我们预处理每个点可以最大向上延展 ...
- poj3250(单调栈模板题)
题目链接:https://vjudge.net/problem/POJ-3250 题意:求序列中每个点右边第一个>=自身的点的下标. 思路:简单介绍单调栈,主要用来求向左/右第一个小于/大于自身 ...
- POJ3250 Bad Hair Day(单调栈)
题目大概就是给一个序列,问每个数右边有几个连续且小于该数的数. 用单调递减栈搞搞就是了. #include<cstdio> #include<cstring> using na ...
- POJ3250(单调栈)
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17614 Accepted: 5937 Des ...
- Bad Hair Day [POJ3250] [单调栈 或 二分+RMQ]
题意Farmer John的奶牛在风中凌乱了它们的发型……每只奶牛都有一个身高hi(1 ≤ hi ≤ 1,000,000,000),现在在这里有一排全部面向右方的奶牛,一共有N只(1 ≤ N ≤ 80 ...
- 【POJ3250】Bad Hair Day 单调栈
题目大意:给定一个由 N 个数组成的序列,求以每个序列为基准,向右最大有多少个数字都比它小. 单调栈 单调栈中维护的是数组的下标. 单调栈在每个元素出栈时统计该出栈元素的答案贡献或对应的值. 单调栈主 ...
- poj3250 Bad Hair Day 单调栈(递减)
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24420 Accepted: 8292 Des ...
随机推荐
- poj 3270(置换群)
题意:给定n头母牛的脾气大小,然后让你通过交换任意两头母牛的位置使得最后的母牛序列的脾气值从小到大,交换两头母牛的代价是两个脾气之和,使得代价最小. 分析:以前做过一道题,只有一个地方和这道题不同,但 ...
- 存储过程中使用事务与try catch
一.存储过程中使用事务的简单语法 在存储过程中使用事务时非常重要的,使用数据可以保持数据的关联完整性,在Sql server存储过程中使用事务也很简单,用一个例子来说明它的语法格式: 代码 : ) ) ...
- GIT 命令 操作 记录
1.git pull时冲突的解决 1.git stash 2.git pull 3.git stash pop stash@{0} 4.修改冲突 5.git add . 6.git commit 7. ...
- 认识Agile,Scrum和DevOps
If everything's under control you are going too slow. 当今的开发,要求faster and faster.所以我们要Agile,become Ag ...
- matlab的&和&&操作
A&B(1)首先判断A的逻辑值,然后判断B的值,然后进行逻辑与的计算.(2)A和B可以为矩阵(e.g. A=[1 0],B=[0 0]).A&&B(1)首先判断A的逻辑值,如果 ...
- [转] 请别再拿“String s = new String("xyz");创建了多少个String实例”来面试了吧
这帖是用来回复高级语言虚拟机圈子里的一个问题,一道Java笔试题的. 本来因为见得太多已经吐槽无力,但这次实在忍不住了就又爆发了一把.写得太长干脆单独开了一帖. 顺带广告:对JVM感兴趣的同学们同志们 ...
- Kali vmtools
参考链接:http://www.leavesongs.com/SHARE/kali-vmtools-install.html 安装环境:kali-1.1.0 ---3.18.0-kali1-amd ...
- Jersey Rest服务类型
在Rest服务中,资源类是接收Rest请求并完成响应的核心类,而资源类由Rest服务的“提供者”来调度的,这一定义类似于自定义Servlet类,该类会奖请求分派给指定的Controller/Actio ...
- mysql 用户权限
创建用户 CREATE USER username IDENTIFIED BY 'password';
- erlang常用命令
1 erlang启动时就运行odbc erl -s odbc 2 ping 节点 net_adm:ping('rabbit@COMPUTERNAME'). 3 运行cmd命令 os:cmd(" ...