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 ≤ h≤ 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

Line 1: The number of cows, N
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i.

Output

Line 1: A single integer that is the sum of c1 through cN.

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【栈】的更多相关文章

  1. Poj 3250 单调栈

    1.Poj 3250  Bad Hair Day 2.链接:http://poj.org/problem?id=3250 3.总结:单调栈 题意:n头牛,当i>j,j在i的右边并且i与j之间的所 ...

  2. poj 3250 Bad Hair Day (单调栈)

    http://poj.org/problem?id=3250 Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  3. poj 3250 Bad Hair Day(栈的运用)

    http://poj.org/problem?id=3250 Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  4. POJ 3250 Bad Hair Day(单调栈)

    [题目链接] http://poj.org/problem?id=3250 [题目大意] 有n头牛,每头牛都有一定的高度,他能看到在离他最近的比他高的牛前面的所有牛 现在每头牛往右看,问每头牛能看到的 ...

  5. POJ 3250 Bad Hair Day --单调栈(单调队列?)

    维护一个单调栈,保持从大到小的顺序,每次加入一个元素都将其推到尽可能栈底,知道碰到一个比他大的,然后res+=tail,说明这个cow的头可以被前面tail个cow看到.如果中间出现一个超级高的,自然 ...

  6. poj 3250 Bad Hair Day 单调栈入门

    Bad Hair Day 题意:给n(n <= 800,000)头牛,每头牛都有一个高度h,每头牛都只能看到右边比它矮的牛的头发,将每头牛看到的牛的头发加起来为多少? 思路:每头要进栈的牛,将栈 ...

  7. poj 3250 Bad Hair Day (单调栈)

    Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14883   Accepted: 4940 Des ...

  8. 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 ...

  9. POJ 3250 Bad Hair Day【单调栈入门】

    Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24112   Accepted: 8208 Des ...

随机推荐

  1. 【形式化方法:VDM++系列】2.VDMTools环境的搭建

    接前文:http://www.cnblogs.com/Kassadin/p/3975853.html 上次讲了软件需求分析的演化过程,本次进入正题——VDM开发环境的搭建 (自从发现能打游戏以来,居然 ...

  2. hdu 3032 Nim or not Nim? 博弈论

     这题是Lasker’s Nim. Clearly the Sprague-Grundy function for the one-pile game satisfies g(0) = 0 and g( ...

  3. winform自定义文件程序-- 不允许所请求的注册表访问权(ZSSQL)

    常见问题1: 不允许所请求的注册表访问权 win7.win8 双击程序文件ZSSQL时候会出现 不允许所请求的注册表访问权 的弹窗异常 解决方法:ZSSQL.exe 右键 属性--兼容性--以管理员身 ...

  4. ajax readyState的五种状态详解

    通过ajax的readyState的值,我们可以知道当前的这个http请求处于什么状态.对于web的调试是比较重要的. readyState 状态说明: (0)未初始化 此阶段确认XMLHttpReq ...

  5. Qt: 把内容写进字符串中与C++很相似(使用QTextStream包装QString)

    #include <iostream>#include <QChar>#include <QFile>#include <QTextStream>#in ...

  6. 在windows下使用git需要反复输入用户名和密码的问题

    节选自我还在写的git文档中的一部分,用md写的,博客园竟然还不支持markdown,完全没有格式啊,懒得弄了,不过解决方法是没有问题的 在win下使用git,如果没有任何设置,一定会反复输入用户名和 ...

  7. P78、面试题10:二进制中1的个数

    题目:请实现一个函数,输入一个整数,输出该数二进制表示中1的个数.例如把9表示成二进制1001,有2位是1.因此如果输入9,该函数输出2. 相关题目: 1)用一条语句判断一个整数是不是2的整数次方.一 ...

  8. 将非WPF window设为 WPF Window的Owner

    如果WPF Content是寄宿在Win32 窗体或Windows Form中,则在WPF模块中可能不会存在WPF Window(WPF模块的根可能是个UserControl).如果在WPF模块中弹出 ...

  9. 最小生成树(kruskal模版 模板)

    题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2144&cid=1186 #include<stdio.h> #incl ...

  10. tap,touch,touchstart,事件与click事件的区别

    根据源码所见, 移动端为了将将单击事件更加灵敏,所以现在的JQM,ST...框架都将JS单击事件封装成tap,或者touch或者touchstart事件, 其实现本质是将click触发多次,以打成移动 ...