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

一些牛从左到右排列。全部的牛都从左往右看,左边的牛仅仅能看到右边的比它身高严格小的牛的发型。假设被一个大于等于它身高的牛挡住。那么它就不能看到再右边的牛。要求每头牛能够看到其它牛的总数。转化一下,事实上就是求每头牛被看到的总次数。能够用单调栈,每次删除栈中比当前牛的身高小于等于的数。事实上这题也能够看做是单调队列。但由于不用对对首操作。所以可看做退化为了栈。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 80600
int a[maxn],stack[maxn];
int main()
{
int n,m,i,j,top;
__int64 sum;
while(scanf("%d",&n)!=EOF){
memset(stack,0,sizeof(stack));
top=0;sum=0;
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
while(top>0 && a[i]>=stack[top])top--;
sum+=top;
stack[++top]=a[i];
}
printf("%I64d\n",sum);
}
return 0;
}

poj3250 Bad Hair Day的更多相关文章

  1. poj3250

    //(栈)poj3250将第i头牛能看到多少牛传化为第i头牛能被多少牛看见 /* #include <stdio.h> #include <stack> using names ...

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

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

  3. bzoj1057,poj3250

    bzoj1057本质上是求最大子矩阵: 第一问是一个经典的O(n2)dp 第二问就是最大子矩阵,回眸一下当年卡了我很久的问题: 首先穷举显然不行(这不废话吗?): 首先我们预处理每个点可以最大向上延展 ...

  4. poj3250(单调栈模板题)

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

  5. POJ3250 Bad Hair Day(单调栈)

    题目大概就是给一个序列,问每个数右边有几个连续且小于该数的数. 用单调递减栈搞搞就是了. #include<cstdio> #include<cstring> using na ...

  6. POJ3250(单调栈)

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

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

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

  8. 【POJ3250】Bad Hair Day 单调栈

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

  9. poj3250 Bad Hair Day 单调栈(递减)

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

随机推荐

  1. php sortable 动态排序

    php sortable 动态排序未分页版.php 预览图: <?php mysql_connect("localhost","root","r ...

  2. JavaScript对象(document对象 图片轮播)

    图片轮播: 需要注意的HTML需要img标签,他和input标签一样,是非封闭的标签 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...

  3. 【LeetCode】202 - Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  4. Tex使用

    表格标题改变成中文的"表": \renewcommand{\tablename}{表} 同样图片的Figure改为中文的“图”: \renewcommand{\figurename ...

  5. 通过ajax提交form表单

    $.ajax({ url : 'deliveryWarrant/update.do', data : $('#myform').serialize(), type : "POST" ...

  6. RVM 安装&卸载

    安装: curl -L https://get.rvm.io | bash -s stable --autolibs=enabled [--ruby] [--rails] [—trace] $ cur ...

  7. MVC5 - ASP.NET Identity登录原理 - Claims-based认证和OWIN -摘自网络

    在Membership系列的最后一篇引入了ASP.NET Identity,看到大家对它还是挺感兴趣的,于是来一篇详解登录原理的文章.本文会涉及到Claims-based(基于声明)的认证,我们会详细 ...

  8. html5页面中拨打电话的方式

    <a href="tel:18688888888">拨号</a> <a href="sms:18688888888">发短信 ...

  9. ESB后台error日志

    本地tomcat没异常 开发环境,生产环境 ESB使用axis2.jar 后台会有错误,但不影响所有流程,该错误源自common.log的error,在捕捉异常后,并未往外继续抛 [ESB 打印] 接 ...

  10. Jenkins+Gitlab搭建持续集成(CI)环境

    利用Jenkins+Gitlab搭建持续集成(CI)环境 Permalink: 2013-09-08 22:04:00 by hyhx2008in intern tags: jenkins gitla ...