(单调栈)Bad Hair Day
来源:互联网 发布:mysql innodb表修复 编辑:程序博客网 时间:2024/05/19 14:51
Bad Hair Day
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 2 Accepted Submission(s) : 1
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.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i.
610374122
5
<pre name="code" class="cpp">#include <cstdio>#include <algorithm>#include <math.h>#include <string.h>using namespace std;#define maxn 80005#define maxint 1000000005int h[maxn],s[maxn];int main(){int N, i;scanf("%d", &N);for (i = 0; i < N; i++)scanf("%d", &h[i]);int top = 0, base = 0;long long ans = 0;s[top++] = N;h[N] = maxint;//虚拟一头hair无限高的牛for (i = N - 1; i >= 0; i--){while (h[s[top - 1]] < h[i])//比当前牛的hair高度小的牛(编号)都出栈top--;ans += s[top - 1] - i-1;s[top++] = i;//只需要将牛的编号入栈便可得出答案}printf("%lld\n", ans);return 0;}
- (单调栈)Bad Hair Day
- poj 3250:Bad Hair Day---单调栈
- poj3250 Bad Hair Day(单调栈)
- POJ 3250 Bad Hair Day(单调栈)
- poj3250-Bad Hair Day(单调栈)
- POJ 3250 Bad Hair Day(单调栈)
- POJ 3250 Bad Hair Day 单调栈
- poj3250 Bad Hair Day 单调栈
- POJ3250 Bad Hair Day 单调栈
- POJ3250---Bad Hair Day(单调栈)
- poj 3250 Bad Hair Day 单调栈
- POJ3250 Bad Hair Day 单调栈
- [单调栈] poj 3250 Bad Hair Day
- POJ 3250 Bad Hair Day 【单调栈】
- poj 3250 Bad Hair Day 【单调栈】
- poj 3250 Bad Hair Day(单调栈)
- POJ3250 Bad Hair Day(单调栈)
- POJ3250 Bad Hair Day(单调栈)
- Java 新特性入门
- 程序员你值得拥有的常用软件
- Game of Connections 卡特兰数
- 【腾讯开源】MT:专注于移动端的JavaScript模块管理框架
- pat1008
- (单调栈)Bad Hair Day
- 关于typedef的用法总结
- 多重继承、派生类
- C# WebService 参数属性
- DevOps=Dev+Ops
- 通向架构师的道路(第七天)之漫谈使用ThreadLocal改进你的层次的划分
- JDK8 部分代码测试
- 项目经验:Query 日期参数的设置方法setTimestamp
- 1626: [Usaco2007 Dec]Building Roads 修建道路 (生成树)