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:

  1.         =
    =       =
    =   -   =         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

  1. 6
  2. 10
  3. 3
  4. 7
  5. 4
  6. 12
  7. 2

Sample Output

  1. 5

【题意】给出一些牛的高度,站成一排,往右看,求所有牛能看见的牛的数量之和。

【思路】单调栈。从左到右取当前牛的高度,从栈顶开始把高度小于或等于当前牛的高度的那些元素删除,此时栈中剩下的元素的数量就是可以看见当前牛的其他牛的数量。把这个数量加在一起,即答案。

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<stack>
  4. #include<string.h>
  5. using namespace std;
  6. int main()
  7. {
  8. stack<int>st;
  9. long long int sum;
  10. int n,h;
  11. while(~scanf("%d",&n))
  12. {
  13. while(!st.empty())
  14. {
  15. st.pop();
  16. }
  17. sum=;
  18. scanf("%d",&h);
  19. st.push(h);
  20. for(int i=;i<n;i++)
  21. {
  22. scanf("%d",&h);
  23. while(!st.empty()&&h>=st.top())
  24. st.pop();
  25. sum+=st.size();
  26. st.push(h);
  27. }
  28. printf("%lld\n",sum);
  29.  
  30. }
  31. return ;
  32. }

Bad Hair Day_单调栈的更多相关文章

  1. [bzoj1660][Usaco2006 Nov]Bad Hair Day_单调栈

    Bad Hair Day bzoj-1660 Usaco-2006 Nov 题目大意:n头牛站成一列,每头牛向后看.f[i]表示第i头牛到第n头牛之间有多少牛,使得这些牛都比i矮,且中间没有比i高的牛 ...

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

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

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

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

  4. BZOJ 3238: [Ahoi2013]差异 [后缀数组 单调栈]

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2326  Solved: 1054[Submit][Status ...

  5. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  6. bzoj1510: [POI2006]Kra-The Disks(单调栈)

    这道题可以O(n)解决,用二分还更慢一点 维护一个单调栈,模拟掉盘子的过程就行了 #include<stdio.h> #include<string.h> #include&l ...

  7. BZOJ1057[ZJOI2007]棋盘制作 [单调栈]

    题目描述 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴阳. 而我们的 ...

  8. 洛谷U4859matrix[单调栈]

    题目描述 给一个元素均为正整数的矩阵,上升矩阵的定义为矩阵中每行.每列都是严格递增的. 求给定矩阵中上升子矩阵的数量. 输入输出格式 输入格式: 第一行两个正整数n.m,表示矩阵的行数.列数. 接下来 ...

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

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

随机推荐

  1. hdu 1598 find the most comfortable road(枚举+卡鲁斯卡尔最小生成树)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  2. 51nod 1613翻硬币

    题目链接:51nod 1613 翻硬币 知乎上的理论解法http://www.zhihu.com/question/26570175/answer/33312310 本题精髓在于奇偶性讨论. 若 n ...

  3. TaskTracker节点上的内存管理器

    Hadoop平台的最大优势就是充分地利用了廉价的PC机,这也就使得集群中的工作节点存在一个重要的问题——节点所在的PC机内存资源有限(这里所说的工作节点指的是TaskTracker节点),执行任务时常 ...

  4. 经典DP 二维换一维

    HDU 1024  Max Sum Plus Plus // dp[i][j] = max(dp[i][j-1], dp[i-1][t]) + num[j] // pre[j-1] 存放dp[i-1] ...

  5. [Jquery]焦点图轮播效果

    $(function(){    var $next=$(".right");    var $prev=$(".left");    var $list_nu ...

  6. EasyMock的原理及使用方法

    就不费劲转过来了https://www.ibm.com/developerworks/cn/opensource/os-cn-easymock/这上面介绍的比较全.

  7. 【第41套测试题NOIP2007】【排序】【DP】【高精度】【树】【图上路径】

    先说点题外话,这两天的入学考试,炸了……语文有史以来最差,数学有史以来最差……还有4科,估计全炸……悲痛的心情,来调程序.这套题是8.31考的,从昨天晚上开始改的,因为第三题迟迟不想写,才拖到了现在. ...

  8. HDU 5382 莫比乌斯反演

    题目大意: 求S(n)的值 n<=1000000 这是官方题解给出的推导过程,orz,按这上面说的来写,就不难了 这里需要思考的就是G(n)这个如何利用积性函数的性质线性筛出来 作为一个质数,那 ...

  9. Gartner报告:多数CIO还未对数字化做好准备

    数字化经济时代已经来临.对于消费者而言,这意味着他们能够随时随地以更加丰富多彩的方式与虚拟世界和现实世界进行互动.对于企业而言,这意味着它们的运营将发生巨大变化,同时也有机会更加深入地了解客户并将这些 ...

  10. win10 用微软账户登录无法访问共享的问题

    百度找了一大堆可以解决的,最终最简单的方式(可能是bug): 测试了一下,Win10用微软账户登录的,连局域网共享时,输入用户名的时候,前面加个乱七八糟的域名就可以访问了: 比如: 用户名:   ba ...