Bad Hair Day

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 24112   Accepted: 8208

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:

  1.         =
  2. =       =
  3. =   -   =         Cows facing right -->
  4. =   =   =
  5. = - = = =
  6. = = = = = =
  7. 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<cstdio> //EOF,NULL
  3. #include<cstring> //memset
  4. #include<cstdlib> //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
  5. #include<cmath> //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
  6. #include<algorithm> //fill,reverse,next_permutation,__gcd,
  7. #include<string>
  8. #include<vector>
  9. #include<queue>
  10. #include<stack>
  11. #include<utility>
  12. #include<iterator>
  13. #include<iomanip> //setw(set_min_width),setfill(char),setprecision(n),fixed,
  14. #include<functional>
  15. #include<map>
  16. #include<set>
  17. #include<limits.h> //INT_MAX
  18. #include<cmath> // bitset<?> n
  19. using namespace std;
  20. #define rep(i,a,n) for(int i=a;i<n;i++)
  21. #define per(i,a,n) for(int i=n-1;i>=a;i--)
  22. #define memset(x,y) memset(x,y,sizeof(x))
  23. #define memcpy(x,y) memcpy(x,y,sizeof(y))
  24. #define all(x) x.begin(),x.end()
  25. #define readc(x) scanf("%c",&x)
  26. #define read(x) scanf("%d",&x)
  27. #define read2(x,y) scanf("%d%d",&x,&y)
  28. #define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
  29. #define print(x) printf("%d\n",x)
  30. #define lowbit(x) x&-x
  31. #define lson(x) x<<1
  32. #define rson(x) x<<1|1
  33. #define pb push_back
  34. #define mp make_pair
  35. #define N 100001
  36. typedef pair<int,int> P;
  37. typedef long long LL;
  38. typedef long long ll;
  39. const double eps=1e-;
  40. const double PI = acos(1.0);
  41. const int INF = 0x3f3f3f3f;
  42. const int inf = 0x3f3f3f3f;
  43. const int mod = 1e9+;
  44. const int MAXN = 1e6+;
  45. const int maxm = ;
  46. const int maxn = + ;
  47. const ll MOD = ;
  48. int a[maxn];
  49. int st[maxn];
  50. ll ans;
  51. int n;
  52.  
  53. int main()
  54. {
  55. while(read(n) != EOF){
  56. memset(st,);
  57. for (int i = ; i < n; i++)
  58. read(a[i]);
  59. a[n] = inf ;
  60. ans = ;
  61. int top = ;
  62. for (int i = ; i <= n; i++){
  63. if (top == || a[i] < a[st[top - ] ])
  64. st[top++] = i;
  65. else{
  66. while (top >= && a[i] >= a[st[top - ]]){
  67. ans += (i - (st[top - ] + ) );
  68. top--;
  69. }
  70. st[top++] = i;
  71. }
  72. }
  73. printf("%lld\n", ans);
  74. }
  75. return ;
  76. }

POJ 3250 Bad Hair Day【单调栈入门】的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. hdu1506 直方图中最大的矩形 单调栈入门

    hdu1506 直方图中最大的矩形 单调栈入门 直方图是由在公共基线对齐的矩形序列组成的多边形.矩形具有相同的宽度,但可能具有不同的高度.例如,左侧的数字显示了由高度为2,1,4,5,1,3,3的矩形 ...

  7. Bad Hair Day POJ - 3250 (单调栈入门题)

    Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-cons ...

  8. POJ 2082 Terrible Sets(单调栈)

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

  9. BZOJ1113 海报PLA1(单调栈入门题)

    一,自己思考下 1,先自己思考下 N个矩形,排成一排,现在希望用尽量少的海报去cover住它们. 2,不懂. 着实不懂. 3,分析下,最优性问题对吧,然后就每什么想法了.. 虽然肯定和单调栈和单调队列 ...

随机推荐

  1. DX9 空间坐标变换示例代码

    // @time 2012.3.25 // @author jadeshu #include <Windows.h> #include <d3d9.h> #include &l ...

  2. Day7 错误和异常

    一.异常 1.异常基础 1.为了让我们的代码在出现异常的时候,整个项目依然是可以正常运行的,所以我们引入了异常处理机制! 2.在编程过程中为了增加友好性,在程序出现bug时一般不会将错误信息显示给用户 ...

  3. [转]LoadRunner 各个指标分析

    转载:https://www.cnblogs.com/dvbbs2012/p/4073635.html 我们要监视CPU,内存.硬盘的资源情况.得到以下的参数提供分析的依据.%processor ti ...

  4. Solid Dominoes Tilings (轮廓线dp打表 + 容器)

    第一步先打一个表,就是利用轮廓线DP去打一个没有管有没有分界线组合数量的表 #include<bits/stdc++.h> using namespace std; ; <<; ...

  5. NSOperation、NSOperationQueue(III)

    NSOperation.NSOperationQueue 常用属性和方法归纳 NSOperation 常用属性和方法 a. 取消操作方法 //可取消操作,实质是标记 isCancelled 状态. - ...

  6. Spark学习之路 (十七)Spark分区

    一.分区的概念 分区是RDD内部并行计算的一个计算单元,RDD的数据集在逻辑上被划分为多个分片,每一个分片称为分区,分区的格式决定了并行计算的粒度,而每个分区的数值计算都是在一个任务中进行的,因此任务 ...

  7. java.security.NoSuchAlgorithmException: AES KeyGenerator not available

    异常信息 Caused by: Java.lang.IllegalStateException: Unable to acquire AES algorithm. This is required t ...

  8. JAVA中获取文件MD5值的方法

    1 DigestUtils.md5Hex(new FileInputStream(path)); 如果你只需要使用标准的MD5,其实一行代码就够了,JAVA自带的commons-codec包就提供了获 ...

  9. HADOOP nutch java mysql

    下载Hadoop安装包 wget  http://apache.fayea.com/hadoop/common/hadoop-2.7.2/hadoop-2.7.2.tar.gz   java安装 wg ...

  10. Numpy 数组简单操作

    创建一个2*2的数组,计算对角线上元素的和 import numpy as np a = np.arange(4).reshape(2,2) print (a) #[[0 1] # [2 3]] n1 ...