Bad Hair Day

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 24420   Accepted: 8292

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

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

题意:有一群牛站成一排,每头牛都是面朝右的,每头牛可以看到他右边身高比他小的牛。给出每头牛的身高,要求每头牛能看到的牛的总数
//单调递减栈
#include<iostream>
#define ll long long
#include<stack>
using namespace std;
stack<ll>p; //栈里面存的是下标
ll a[];
ll n,ans;
int main()
{
while(~scanf("%lld",&n))
{
while(!p.empty())
p.pop();
for(int i=;i<n;i++)
scanf("%lld",&a[i]);
a[n]=;//为找比a[n-1]大的数准备,因为是递减栈,将a[n]设为最大值
ans=;
for(int i=;i<=n;i++)
{
if(p.empty()||a[i]<a[p.top()])//符合严格单调递减规则,入栈
p.push(i);
else
{
while(!p.empty()&&a[i]>=a[p.top()])//找到第一个不小于栈顶元素的数的下标
{
ll top;
top=p.top();
p.pop();
ans=ans+(i-top-);//这个数到第一个不小于这个数之间的数都是比这个数小,开区间
}
//如果a[i]可以使当前栈严格单调递减,入栈
p.push(i);
}
}
printf("%lld\n",ans);
}
return ; }

poj3250 Bad Hair Day 单调栈(递减)的更多相关文章

  1. POJ3250[USACO2006Nov]Bad Hair Day[单调栈]

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

  2. Bad Hair Day [POJ3250] [单调栈 或 二分+RMQ]

    题意Farmer John的奶牛在风中凌乱了它们的发型……每只奶牛都有一个身高hi(1 ≤ hi ≤ 1,000,000,000),现在在这里有一排全部面向右方的奶牛,一共有N只(1 ≤ N ≤ 80 ...

  3. poj3250(单调栈模板题)

    题目链接:https://vjudge.net/problem/POJ-3250 题意:求序列中每个点右边第一个>=自身的点的下标. 思路:简单介绍单调栈,主要用来求向左/右第一个小于/大于自身 ...

  4. [poj3250]单调栈 Bad Hair Day

    解题关键:将每头牛看到的牛头数总和转化为每头牛被看到的次数,然后用单调栈求解,其实做这道题的目的只是熟悉下单调栈 此题为递减栈 #include<cstdio> #include<c ...

  5. POJ3250(单调栈)

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

  6. 【POJ3250】Bad Hair Day 单调栈

    题目大意:给定一个由 N 个数组成的序列,求以每个序列为基准,向右最大有多少个数字都比它小. 单调栈 单调栈中维护的是数组的下标. 单调栈在每个元素出栈时统计该出栈元素的答案贡献或对应的值. 单调栈主 ...

  7. 单调栈2 POJ3250 类似校内选拔I题

    这个题再次证明了单调栈的力量 简单 单调栈 类似上次校内选拔消砖块 一堆牛面朝右排 给出从左到右的 问每个牛的能看到前面牛发型的个数之和 //re原因 因为在执行pop的时候没有判断empty 程序崩 ...

  8. BZOJ 4453: cys就是要拿英魂![后缀数组 ST表 单调栈类似物]

    4453: cys就是要拿英魂! Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 90  Solved: 46[Submit][Status][Discu ...

  9. POJ2796Feel Good[单调栈]

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13376   Accepted: 3719 Case T ...

随机推荐

  1. C#分布式事务解决方案-TransactionScope(转)

    出处:C#分布式事务解决方案-TransactionScope 引用一下别人的导读:在实际开发工作中,执行一个事件,然后调用另一接口插入数据,如果处理逻辑出现异常,那么之前插入的数据将成为垃圾数据,我 ...

  2. js去掉字符串前后空格的五种方法(转)

    出处:http://www.2cto.com/kf/201204/125943.html 第一种:循环检查替换[javascript]//供使用者调用  function trim(s){  retu ...

  3. linux每天一小步---alias命令详解

    1 命令功能     alias命令用来设置指令的别名,alias命令设置的别名只限于该次登陆操作,若要每次登入即自动设好别名,可在/etc/profile或自己的~/.bashrc中设定指令的别名. ...

  4. WPF 绑定备忘单

     Part I – Common Examples Basic Binding   {Binding}  Bind to current DataContext. {Binding Name}  Bi ...

  5. 声音处理(Cool Edit)

    平直化处理 上升半音

  6. VMware安装Ubuntu

    1. VMware Ubuntu安装详细过程:http://blog.csdn.net/u013142781/article/details/50529030 2. 怎样在VMware虚拟机中使用安装 ...

  7. (巴什博弈 sg函数入门1) Brave Game -- hdu -- 1846

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1846 首先来玩个游戏,引用杭电课件上的: (1) 玩家:2人:(2) 道具:23张扑克牌:(3) 规则: ...

  8. 解决PL/SQL Developer连接数据库时出现 “ORA-12541:TNS:无监听程序”错误

    在用PL/SQL Developer连接数据库时出现“ORA-12541:TNS:无监听程序”错误. 1.检查listener.log日志发现下面错误:TNSLSNR for 32-bit Windo ...

  9. long polling

    Regular http: client 发出请求到server server 计算 response server 响应 response 给 client Polling: A client re ...

  10. jQuery outerHeight() 方法

    outerHeight() 方法返回第一个匹配元素的外部高度. 如下面的图像所示,该方法包含 padding 和 border. 提示:如需包含 margin,请使用 outerHeight(true ...