POJ 3250 Bad Hair Day【单调栈入门】
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:
=
= =
= - = 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<cstdio> //EOF,NULL
#include<cstring> //memset
#include<cstdlib> //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
#include<cmath> //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
#include<algorithm> //fill,reverse,next_permutation,__gcd,
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<utility>
#include<iterator>
#include<iomanip> //setw(set_min_width),setfill(char),setprecision(n),fixed,
#include<functional>
#include<map>
#include<set>
#include<limits.h> //INT_MAX
#include<cmath> // bitset<?> n
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(y))
#define all(x) x.begin(),x.end()
#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
#define pb push_back
#define mp make_pair
#define N 100001
typedef pair<int,int> P;
typedef long long LL;
typedef long long ll;
const double eps=1e-;
const double PI = acos(1.0);
const int INF = 0x3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int MAXN = 1e6+;
const int maxm = ;
const int maxn = + ;
const ll MOD = ;
int a[maxn];
int st[maxn];
ll ans;
int n; int main()
{
while(read(n) != EOF){
memset(st,);
for (int i = ; i < n; i++)
read(a[i]);
a[n] = inf ;
ans = ;
int top = ;
for (int i = ; i <= n; i++){
if (top == || a[i] < a[st[top - ] ])
st[top++] = i;
else{
while (top >= && a[i] >= a[st[top - ]]){
ans += (i - (st[top - ] + ) );
top--;
}
st[top++] = i;
}
}
printf("%lld\n", ans);
}
return ;
}
POJ 3250 Bad Hair Day【单调栈入门】的更多相关文章
- poj 3250 Bad Hair Day 单调栈入门
Bad Hair Day 题意:给n(n <= 800,000)头牛,每头牛都有一个高度h,每头牛都只能看到右边比它矮的牛的头发,将每头牛看到的牛的头发加起来为多少? 思路:每头要进栈的牛,将栈 ...
- poj 3250 Bad Hair Day (单调栈)
http://poj.org/problem?id=3250 Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- poj 3250 Bad Hair Day (单调栈)
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14883 Accepted: 4940 Des ...
- POJ 3250 Bad Hair Day --单调栈(单调队列?)
维护一个单调栈,保持从大到小的顺序,每次加入一个元素都将其推到尽可能栈底,知道碰到一个比他大的,然后res+=tail,说明这个cow的头可以被前面tail个cow看到.如果中间出现一个超级高的,自然 ...
- poj 2769 感觉♂良好 (单调栈)
poj 2769 感觉♂良好 (单调栈) 比尔正在研发一种关于人类情感的新数学理论.他最近致力于研究一个日子的好坏,如何影响人们对某个时期的回忆. 比尔为人的一天赋予了一个正整数值. 比尔称这个值为当 ...
- hdu1506 直方图中最大的矩形 单调栈入门
hdu1506 直方图中最大的矩形 单调栈入门 直方图是由在公共基线对齐的矩形序列组成的多边形.矩形具有相同的宽度,但可能具有不同的高度.例如,左侧的数字显示了由高度为2,1,4,5,1,3,3的矩形 ...
- 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 ...
- POJ 2082 Terrible Sets(单调栈)
[题目链接] http://poj.org/problem?id=2082 [题目大意] 给出一些长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题解] 我们 ...
- BZOJ1113 海报PLA1(单调栈入门题)
一,自己思考下 1,先自己思考下 N个矩形,排成一排,现在希望用尽量少的海报去cover住它们. 2,不懂. 着实不懂. 3,分析下,最优性问题对吧,然后就每什么想法了.. 虽然肯定和单调栈和单调队列 ...
随机推荐
- 鼠标移动上去,元素旋转;web前端鼠标经过图片凸起
.trans-rotate{ -webkit-transition: transform .25s linear; -moz-transition: transform .25s linear; -o ...
- html5-figure和figcaption元素
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 参与.net开源项目开发
EntityFramework6 https://github.com/aspnet/EntityFramework6 https://github.com/aspnet/EntityFramewor ...
- HCatalog 学习之路
最近在使用sqoop把数据从hive数仓导出到mysql数据库中接触到了hcatalog,所以特意学习了解一下相关知识,据悉hcatalog还是apache顶级项目. 学习参考: HCatalog 介 ...
- bootsrtap h5 移动版页面 在苹果手机ios滑动上下拉动滚动卡顿问题解决方法
bootsrtap h5 移动版页面 在苹果手机ios滑动上下拉动滚动卡顿问题解决方法 bootsrtap框架做的h5页面,在android手机下没有卡顿问题,在苹果手机就一直存在这问题,开始毫无头绪 ...
- 两眼论&矩阵变现理论结合打造赚钱大模式
两眼论&矩阵变现理论结合打造赚钱大模式 围棋有一个基本规则,就是一块棋有两只真眼,就是活棋. 围棋没有复杂的规则,它最有趣的地方是没有太多的规则和限制,由此演变出了大千世界,所以古人云“棋如人 ...
- Lucene 个人领悟 (三)
其实接下来就是贴一下代码,熟悉一下Lucene的正常工作流程,或者说怎么使用这个API,更深层次的东西这篇文章不会讲到. 上一篇文章也说了maven的配置,只要你电脑联网就可以下载下来.我贴一下代码. ...
- ubuntu16.04 无法连接wifi和校园宽带问题的解决办法
我遇到的问题是在ubuntu16.04系统下无法进行上海大学校园宽带连接或者校园wifi连接,我一个一个来解决这两个问题. 1.无法连接校园宽带的问题:输入校园账号和密码后,宽带始终连接不上.(上海大 ...
- LNMP的并发配置和资源分配
PHP程序性能如何?程序的并发可以达到多少?程序的瓶颈在哪儿?为了满足业务需求应该购买多少台服务器?负载均衡中php应用服务器需要多少台? LNMP中的N是nginx充当Web Server 内容的分 ...
- 前端框架VUE----webpack打包工具的使用
在这里我仅仅的是对webpack做个讲解,webpack这个工具非常强大,解决了我们前端很繁琐的一些工具流程繁琐的事情.如果感兴趣的同学,还是看官网吧. 中文链接地址:https://www.webp ...