Bad Hair Day

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17614   Accepted: 5937

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

一群高度不完全相同的牛从左到右站成一排,每头牛只能看见它右边的比它矮的牛的发型,若遇到一头高度大于或等于它的牛,则无法继续看到这头牛后面的其他牛。

给出这些牛的高度,要求每头牛可以看到的牛的数量的和。

把要求作一下转换,其实就是要求每头牛被看到的次数之和。这个可以使用单调栈来解决。

从左到右依次读取当前牛的高度,从栈顶开始把高度小于或等于当前牛的高度的那些元素删除,此时栈中剩下的元素的数量就是可以看见当前牛的其他牛的数量。把这个数量加在一起,就可以得到最后的答案了。

单调栈的代码最后竟那么简洁。

 //2016.8.23
#include<cstdio> const int N = ;
int Stack[N], hi, top; int main()
{
int n;
long long ans;
while(scanf("%d", &n)!=EOF)
{
ans = top = ;
for(int i = ; i <= n; i++)
{
scanf("%d", &hi);
while(top>&&Stack[top-]<=hi)top--;
ans += top;
Stack[top++] = hi;
}
printf("%lld\n", ans);
} return ;
}

POJ3250(单调栈)的更多相关文章

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

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

  2. [poj3250]单调栈 Bad Hair Day

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

  3. poj3250单调栈

    有n只羊,(姑且算是羊吧,也有可能是牛啊猫啊什么之类的),每只羊都有一个身高,前面的羊要看到后面的羊的条件是,后面的羊高度要小于前面的羊,就问各位羊加起来看到的牛多少只....... #include ...

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

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

  5. poj3250(单调栈模板题)

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

  6. 【POJ3250】Bad Hair Day 单调栈

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

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

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

  8. 「日常训练&知识学习」单调栈

    这几天的知识学习比较多,因为时间不够了.加油吧,为了梦想. 这里写几条简单的单调栈作为题解记录,因为单调栈的用法很简单,可是想到并转化成用这个需要一些题目的积淀. 相关博客参见:https://blo ...

  9. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

随机推荐

  1. PAT (Advanced Level) 1010. Radix (25)

    撸完这题,感觉被掏空. 由于进制可能大的飞起..所以需要开longlong存,答案可以二分得到. 进制很大,导致转换成10进制的时候可能爆long long,在二分的时候,如果溢出了,那么上界=mid ...

  2. 我的云服务之WWW

    云服务器系统是:[root@ip-172-31-27-132 system]# cat /etc/redhat-release Red Hat Enterprise Linux Server rele ...

  3. linux iptables 相关设置

    首先在使用iptables之前敲入一下两条命令 > iptables -F   #这句话的意思是清空所有的链 > iptables -X  #这句话的意思是清空所有自定义的链 以上两条的含 ...

  4. 【转】C++中this指针的用法详解

    1.this指针的用处 一个对象的this指针并不是对象本身的一部分,不会影响sizeof(对象)的结果.this作用域是在类内部,当在类的非静态成员函数中访问类的非静态成员的时候,编译器会自动将对象 ...

  5. November 11th 2016 Week 46th Friday

    Keep in mind that neither success nor failure is ever final. 无论成败,皆非定局. The final is not coming, but ...

  6. (中等) HDU 1542 Atlantis,扫描线。

    Problem Description There are several ancient Greek texts that contain descriptions of the fabled is ...

  7. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  8. 《玩转Bootstrap(JS插件篇)》笔记

    导入JavaScript插件 不论是单独导入还一次性导入之前必须先导入jQuery库. 一次性导入 <script src="js/bootstrap.min.js"> ...

  9. 功率W与dBm的对照表及关系(转)

    源:功率W与dBm的对照表及关系 功率W与dBm的对照表 dBm          Watts                          dBm            Watts  0     ...

  10. css--用户体验笔记及兼容记录

    css用户体验 1.HTML的< >&"©分别是<,>,&,",©;的转义字符 2.IE8和7的透明度 filter: progid:DXI ...