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

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

单调栈的入门题:单调栈就是维护一个栈,栈中的元素都保持着单调递增或递减的顺序。

题目意思:有n仅仅牛站在一排,给出队伍中每仅仅牛的高度。每仅仅牛仅仅能看到它右边比它矮的牛。求全部的牛能看到的牛数之和。

当我们新增加一个高度值时,假设栈中存在元素小于新增加的高度值。那这个牛肯定看不见这个高度的牛,就把这个元素弹栈。每次增加新元素,并运行完弹出操作后,栈中元素个数便是能够看见这个牛的“牛数”。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
typedef long long ll;
using namespace std;
int main()
{
int n;
ll heigh,ans;
stack<ll>s;
while(scanf("%d",&n)!=EOF)
{
ans=0;
cin>>heigh;
s.push(heigh);//入栈
for(int i=1;i<n;i++)
{
cin>>heigh;
while(!s.empty()&&s.top()<=heigh) //比較栈顶元素和新增加元素的关系
{
s.pop();
}
ans+=s.size();
s.push(heigh);
}
cout<<ans<<endl;
while(!s.empty()) s.pop();//空栈
}
return 0;
}

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

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

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

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

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

  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. [置顶] Oracle 11g Data Guard Role Transitions: Failover

    Role TransitionsInvolving Physical Standby Databases A database operates in one of the following mut ...

  2. android之写文件到sd卡

    1.main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...

  3. xp下Oracle数据库导入SQLServer数据库数据

    Oracle数据库利用ODBC数据源.PLSQL Developer导入SQLServer数据库数据 操作: 建立数据源:控制面板→管理工具→数据源 (ODBC) 打开,界面如下: 点击添加,界面如下 ...

  4. HDU 1007 近期点对

    分治法求近期点对 递归将点不断分成小组.计算最短距离.此时的最短距离不过两点都属两块的某一块(这里的切割点是mid点). 还须要考虑两点分属两块的情况. 这时对于选点则把范围缩小到了以mid为中心. ...

  5. sql plus 抢救数据(測)

    有索引 --运行plsql脚本 DECLARE  TYPE RowIDTab ISTABLE OF ROWID INDEXBY BINARY_INTEGER;    CURSORc1 IS selec ...

  6. Java原型模式之基础

    一.是什么? 定义:用原型实例指定创建对象的种类,而且通过拷贝这些原型创建新的对象.(官方定义) 原型模式主要用于对象的复制,它的核心是就是类图中的原型类Prototype. Prototype类须要 ...

  7. margin 等高布局

    <div id="main"> <div id="left"> 我是左边的内容的啦啦啦啦... .<br> 我是左边的内容的 ...

  8. 内存级别/栅栏 ( Memory Barriers / Fences ) – 翻译

    翻译自:Martin Thompson – Memory Barriers/Fences 在这篇文章里,我将讨论并发编程里最基础的技术–以内存关卡或栅栏著称.那让进程内的内存状态对其它进程可见. CP ...

  9. c#操作.mpp文件

    原文地址:http://mjm13.iteye.com/blog/532404 所需设置    在工程中增加引用Microsoft Project 11.0 Object Library,该引用在co ...

  10. 轻松学习之Linux教程六 正則表達式具体解释

    本系列文章由@超人爱因斯坦出品.转载请注明出处. 作者:超人爱因斯坦    个人站点:http://www.hpw123.net          文章链接:http://hpw123.net/a/L ...