poj3250 Bad Hair Day

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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-24 20:45:54

poj3250 Bad Hair Day的相关文章

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

Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17774   Accepted: 6000 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

poj3250

//(栈)poj3250将第i头牛能看到多少牛传化为第i头牛能被多少牛看见 /* #include <stdio.h> #include <stack> using namespace std; int main(){ unsigned long N,ans=0; stack<unsigned long>s; scanf("%ld",&N); while(N--){ unsigned long cowHigh; scanf("%ld

[poj3250]单调栈 Bad Hair Day

解题关键:将每头牛看到的牛头数总和转化为每头牛被看到的次数,然后用单调栈求解,其实做这道题的目的只是熟悉下单调栈 此题为递减栈 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<stack> 6 #include<iostream> 7 using namespace std; 8 typedef lo

POJ3250(单调栈)

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

机试练习02:poj3250——Bad Hair Day

参考来源:https://blog.csdn.net/htt_h/article/details/39716905 一.题解方法 维护一个单调递减队列,如果输入元素大于队尾元素,则清空队列.因此,队尾减队头就是所有矮于当前身高的奶牛数. 二.题解代码 1 #include <iostream> 2 3 using namespace std; 4 5 6 int que[80000]; 7 int head = 0, rear = -1; 8 9 int main() 10 { 11 int

DP总结 ——QPH

常见优化 单调队列 形式 dp[i]=min{f(k)} dp[i]=max{f(k)} 要求 f(k)是关于k的函数 k的范围和i有关 转移方法 维护一个单调递增(减)的队列,可以在两头弹出元素,一头压入元素. 队列中维护的是两个值.一个是位置,这和k的范围有关系,另外一个是f(k)的值,这个用来维护单调性,当然如果f(k)的值可以利用dp值在O(1)的时间内计算出来的话队列中可以只维护一个表示位置的变量. 枚举到一个i的时候,首先判断队首元素的位置是否已经不满足k的范围了,如果不满足就将队首

POJ3250---Bad Hair Day(单调栈)

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 sp

单调栈练习题题解

单调栈 单调栈顾名思义就是让栈中的元素是单调的,要么递增,要么递减.同样它也满足栈的性质,先进后出. 单调递增栈,则从栈顶到栈底的元素是严格递增的 单调递减栈,则从栈顶到栈底的元素是严格递减的 练习题 单调栈 练习题 POJ3250 POJ2796 BZOJ1113 HDU1506 POJ2559 JDFZ2997 POJ3250 POJ3250传送门 对于每一个牛来说,能看到的数目为向右数身高比它小的个数,累加就是答案. 所以可以倒着维护一个单调递增的栈,记录i之前的弹栈数目,累加. (正着也