http://poj.org/problem?id=3250

Bad Hair Day
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11473   Accepted: 3871

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

Source

 
 
思路:
暴力N*N肯定会超时,所以就想怎样通过一次遍历就求出结果呢
这里用到单调栈
struct Nod
{
    __int64 height;
    __int64 startPos;
}node;
定义节点中包含高度height,以及所在的位置
对于 输入的 数据:10  3  7  4  12  2
初始化栈: 先在栈中push进一个节点(inf,-1)以方便后面节点的统一处理,inf为一个无限大的数
 
 i=0时 10<inf  故压入栈  (输入值跟栈顶元素比较)
(inf,-1)  (10,0) 
 
 i=1时 3<10  压入栈
(inf,-1)  (10,0)  (3,1)
 
i=2时  7>3  出栈并计算     sum+=i-H1.startPos-1;    直到 7<10  压栈
(inf,-1)  (10,0)   (7,2)
 
i=3时 4<7  压栈
(inf,-1)  (10,0)   (7,2)  (4,3)
 
i=4时  12>4  出栈并计算     sum+=i-H3.startPos-1;
          12>7  出栈并计算    sum+=i-H2.startPos-1;
    12>10  出栈并计算    sum+=i-H0.startPos-1;
          12<inf  入栈
(inf,-1)  (12,4)
 
i=5时 2<12  压栈
(inf,-1)  (12,4)   (2,5)
 
最后将栈内除了(inf,-1)的其他节点出栈并计算
(2,5)出栈   sum+=i-H5.startPos-1;
(12,4) 出栈    sum+=i-H4.startPos-1;
 
最后得到的sum即为所求。
 
代码C/C++:
 #include<iostream>
#include<stdio.h>
#include<stack> #define INF 1000000001 using namespace std; struct Nod
{
__int64 height;
__int64 startPos;
}node; int main()
{
__int64 n;
while(~scanf("%I64d",&n))
{
__int64 i,a,sum=;
stack <Nod> stk;
node.height = INF;
node.startPos = -;
stk.push(node);
for(i=;i<n;i++)
{
scanf("%I64d",&a);
while(a>=stk.top().height)
{
node = stk.top();
stk.pop();
sum += i - node.startPos - ;
}
node.height = a;
node.startPos = i;
stk.push(node);
}
while(!stk.empty())
{
node = stk.top();
stk.pop();
if(node.height!=INF) sum += i - node.startPos - ;
}
printf("%I64d\n",sum);
}
return ;
}
 

poj 3250 Bad Hair Day (单调栈)的更多相关文章

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

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

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

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

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

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

  4. poj 2769 感觉♂良好 (单调栈)

    poj 2769 感觉♂良好 (单调栈) 比尔正在研发一种关于人类情感的新数学理论.他最近致力于研究一个日子的好坏,如何影响人们对某个时期的回忆. 比尔为人的一天赋予了一个正整数值. 比尔称这个值为当 ...

  5. POJ 2082 Terrible Sets(单调栈)

    [题目链接] http://poj.org/problem?id=2082 [题目大意] 给出一些长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题解] 我们 ...

  6. POJ 2796 Feel Good 【单调栈】

    传送门:http://poj.org/problem?id=2796 题意:给你一串数字,需要你求出(某个子区间乘以这段区间中的最小值)所得到的最大值 例子: 6 3 1 6 4 5 2 当L=3,R ...

  7. POJ 2796 Feel Good(单调栈)

    传送门 Description Bill is developing a new mathematical theory for human emotions. His recent investig ...

  8. Feel Good POJ - 2796 (前缀和+单调栈)(详解)

    Bill is developing a new mathematical theory for human emotions. His recent investigations are dedic ...

  9. 51nod 1215 数组的宽度&poj 2796 Feel Good(单调栈)

    单调栈求每个数在哪些区间是最值的经典操作. 把数一个一个丢进单调栈,弹出的时候[st[top-1]+1,i-1]这段区间就是弹出的数为最值的区间. poj2796 弹出的时候更新答案即可 #inclu ...

随机推荐

  1. 圆满完成平安科技Web安全与App应用安全测试培训!

    圆满完成平安科技Web安全与App应用安全测试培训!

  2. matlab——sparse函数和full函数(稀疏矩阵和非稀疏矩阵转换)

    函数功能:生成稀疏矩阵 使用方法 :S = sparse(A) 将矩阵A转化为稀疏矩阵形式,即矩阵A中任何0元素被去除,非零元素及其下标组成矩阵S.如果A本身是稀疏的,sparse(S)返回S. S ...

  3. Oracle计算连续天数,计算连续时间,Oracle连续天数统计

    Oracle计算连续天数,计算连续时间,Oracle连续天数统计 >>>>>>>>>>>>>>>>> ...

  4. Centos 6.4 安装elasticsearch+kibana

    elasticsearch和kibanna的链接地址:https://www.elastic.co/downloads,我的环境里用的包为kibana-4.1.1-linux-x64.tar.gz和e ...

  5. wsdl 关于nillable和minOccurs 在.NET和java中的不同

    术语约定文章中会反复出现[值类型].[包装类型].[普通引用类型].[元素节点]和[元素取值]的表述1> [值类型]指的是java和.NET中的基本数据类型,如:int:2> [包装类型] ...

  6. 用js操作表格

    效果图: 任务: 1. 鼠标移到不同行上时背景色改为色值为 #f2f2f2,移开鼠标时则恢复为原背景色 #fff var tr=document.getElementsByTagName(" ...

  7. 一点关于this的理解

    关于this,是很多前端面试必考的题目,有时候在网上看到这些题目,自己试了一下,额,还真的错了!在实际开发中,也会遇到 this 的问题(虽然一些类库会帮我们处理),例如在使用一些框架的时候,例如:k ...

  8. 주기적으로 php파일 실행시키기 (PHP 파일 cron 으로 돌리기)

    크론탭에 추가 ]# crontab -e       한시간에 한번씩 원하는 페이지를 실행시키는 코드 0 * * * * wget -O - -q -t 1 http://domain.com ...

  9. Spring之IOC容器初始化过程

    Ioc容器的初始化是由refresh()方法来启动的,这个方法标志着Ioc容器的正式启动. 具体来说这个启动过程包括三个基本过程: 1.BeanDifinition的Resource定位 2.Bean ...

  10. Delphi - 在ListView中添加一个进度条

    // 相关定义 Type TListData = Record FileName: String; Percent: Integer; End; PListData = ^TListData; // ...