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 ≤ 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:
=
= =
= - = 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
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i.
Output
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的更多相关文章
- poj3250
//(栈)poj3250将第i头牛能看到多少牛传化为第i头牛能被多少牛看见 /* #include <stdio.h> #include <stack> using names ...
- POJ3250[USACO2006Nov]Bad Hair Day[单调栈]
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17774 Accepted: 6000 Des ...
- bzoj1057,poj3250
bzoj1057本质上是求最大子矩阵: 第一问是一个经典的O(n2)dp 第二问就是最大子矩阵,回眸一下当年卡了我很久的问题: 首先穷举显然不行(这不废话吗?): 首先我们预处理每个点可以最大向上延展 ...
- poj3250(单调栈模板题)
题目链接:https://vjudge.net/problem/POJ-3250 题意:求序列中每个点右边第一个>=自身的点的下标. 思路:简单介绍单调栈,主要用来求向左/右第一个小于/大于自身 ...
- POJ3250 Bad Hair Day(单调栈)
题目大概就是给一个序列,问每个数右边有几个连续且小于该数的数. 用单调递减栈搞搞就是了. #include<cstdio> #include<cstring> using na ...
- POJ3250(单调栈)
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17614 Accepted: 5937 Des ...
- Bad Hair Day [POJ3250] [单调栈 或 二分+RMQ]
题意Farmer John的奶牛在风中凌乱了它们的发型……每只奶牛都有一个身高hi(1 ≤ hi ≤ 1,000,000,000),现在在这里有一排全部面向右方的奶牛,一共有N只(1 ≤ N ≤ 80 ...
- 【POJ3250】Bad Hair Day 单调栈
题目大意:给定一个由 N 个数组成的序列,求以每个序列为基准,向右最大有多少个数字都比它小. 单调栈 单调栈中维护的是数组的下标. 单调栈在每个元素出栈时统计该出栈元素的答案贡献或对应的值. 单调栈主 ...
- poj3250 Bad Hair Day 单调栈(递减)
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24420 Accepted: 8292 Des ...
随机推荐
- bjfu1253 最大上升子序列和
n^2的算法就行,很简单的动态规划.直接上代码 /* * Author : ben */ #include <cstdio> #include <cstdlib> #inclu ...
- 自己写了一个类似百度空间自动保存草稿的程序 php+jquery
可以异步加载mysql中的草稿~,异步更新草稿列表~ 下载地址:http://download.csdn.net/source/3479156 代码: demo.php <?php mysql_ ...
- windows下django1.7 +python3.4.2搭建记录2
1.自定义页面写一个显示当前时间的页面views.py文件加一个使用模板的模块,整体内容为: #coding=utf-8 from django.shortcuts import render fro ...
- 再来说说Activity
经过前面多天的了解,现在可以确信一点: activity提供了用户和程序交互的界面. 而且android里有四大组件:Activity,Service,BroadcastReceiver,Conten ...
- Apriori算法例子
1 Apriori介绍 Apriori算法使用频繁项集的先验知识,使用一种称作逐层搜索的迭代方法,k项集用于探索(k+1)项集.首先,通过扫描事务(交易)记录,找出所有的频繁1项集,该集合记做L1,然 ...
- string subscript out of range
刚刚练习华为机试上的题目遇到了这个问题,奉上两个小题: //题目描述 // //描述: //输入一个整数,将这个整数以字符串的形式逆序输出 //程序不考虑负数的情况,若数字含有0,则逆序形式也含有0, ...
- 轻松学Linux之使用转义字符
转义字符是C语言中表示字符的一种特殊形式.通常使用转义字符表示ASCII码字符集中不可打印的控制字符和特定功能的字符,如用于表示字符常量的单撇号( '),在Unix操作系统中有一类特殊的字符 ...
- Test log4net
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGloba ...
- Hadoop概念学习系列之分布式数据集的容错性(二十七)
一般来说,分布式数据集的容错性有两种方式: 1.数据检查点 2.记录数据的更新 我们面向的是大规模数据分析,数据检查点操作成本很高:需要通过数据中心的网络连接在机器之间复制庞大的数据集,而网络带宽往往 ...
- CSRF的攻击与防御(转)
add by zhj:CSRF之所有发生,是因为http请求中会自动带上cookies,我的解决办法是:前端不要将数据放在cookie中,而是放在其它本地存储 (HTML5中称之为Web Storag ...