POJ 3250:Bad Hair Day 好玩的单调栈
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 15699 | Accepted: 5255 |
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 <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; #define N 80002 long long a[N], stack[N], top; int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); long long ans, tmp;
int i, j, n; scanf("%d", &n); for (i = 1; i <= n; i++)
{
scanf("%lld", a + i);
}
a[++n] = 1e9 + 7;
top = 0;
ans = 0;
for (i = 1; i <= n; i++)
{
while (top >= 1 && a[i] >= a[stack[top - 1]])
{
--top;
}
ans = ans + top;
if (top == 0 || a[i] < a[stack[top - 1]])
{
stack[top++] = i;
}
}
printf("%lld\n", ans); //system("pause");
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 3250:Bad Hair Day 好玩的单调栈的更多相关文章
- poj 3415 Common Substrings(后缀数组+单调栈)
http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K Total Sub ...
- 【POJ】2796:Feel Good【单调栈】
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 18449 Accepted: 5125 Case T ...
- POJ 3415 Common Substrings(后缀数组 + 单调栈)题解
题意: 给两个串\(A.B\),问你长度\(>=k\)的有几对公共子串 思路: 先想一个朴素算法: 把\(B\)接在\(A\)后面,然后去跑后缀数组,得到\(height\)数组,那么直接\(r ...
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...
- POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15831 ...
- POJ.3145.Common Substrings(后缀数组 倍增 单调栈)
题目链接 \(Description\) 求两个字符串长度不小于k的公共子串对数. \(Solution\) 求出ht[]后先减去k,这样对于两个后缀A',B',它们之间的贡献为min{ht(A)}( ...
- 题解 POJ 2559【Largest Rectangle in a Histogram】(单调栈)
题目链接:http://poj.org/problem?id=2559 思路:单调栈 什么是单调栈? 单调栈,顾名思义,就是单调的栈,也就是占中存的东西永远是单调(也就是递增或递减)的 如何实现一个单 ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈) && 单调栈
嗯... 题目链接:http://poj.org/problem?id=2559 一.单调栈: 1.性质: 单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递 ...
随机推荐
- 五、linux基础-shell机制
5.1 shell机制1.Linux命令程序员可以看懂,但是操作系统是不懂这句话的含义的.因为所有的命令必须重新被解释然后传递给Linux内核才可以执行.这一被解释的机制就是shell. Linux命 ...
- SpringMVC配置没有任何问题根据请求却怎么都找不到映射(tomcat所导致的问题)
本人在做SpringMVC练习的时候,配置文件反复检查了不下十几遍,没有任何问题,然后就招了个之前的项目的源码复制进去,原来的项目没有任何问题,这个项目却怎么都不能跳转路径,然后有找了一个Spring ...
- cookie、session、localStorage、sessionStorage的区别
cookie的机制 cookie是存储在用户本地终端上的数据.有时也用cookies,指某些网站为了辨别用户身份,进行session跟踪而存储在本地终端上的数据,通常经过加密. Cookie是服务器发 ...
- Hot Module Replacement [热模块替换]
安装了webpack-dev-server后 , 配置 "start": "webpack-dev-server" 然后运行 npm start 会开起一个we ...
- SSIS 無法將保護的 XML 節點 "DTS:Password" 解密,錯誤為 0x8009000B "機碼用在特定狀態時無效
发现之前部署的SSIS,执行失败,查看日志 來源: 描述: 無法將保護的 XML 節點 -- ::-- ::-- :: DataReader 來源 [] 描述: System.Exception: S ...
- matlab练习程序(龙格库塔法)
非刚性常微分方程的数值解法通常会用四阶龙格库塔算法,其matlab函数对应ode45. 对于dy/dx = f(x,y),y(0)=y0. 其四阶龙格库塔公式如下: 对于通常计算,四阶已经够用,四阶以 ...
- 【C++初学者自学笔记一】(把自己刚学到的东西做一下总结,如果有哪些地方不足请留言指正)
这是我写的第一个博客关于C++的一些笔记,我不会写的太深奥,因为这样很多人会看不懂(我刚开始学C语言深受其害).个人觉得C++这门语言有些类似于C语言但是有些函数的用法还是有不一样的.C语言中的头文件 ...
- EcShop二次开发学习方法
EcShop二次开发学习方法 (2012-03-08 11:10:08) 转载▼ 标签: 京东 公用函数库 二次开发 sql语言 数据库设计 杂谈 分类: ecshop 近年来,随着互联网的发展,电子 ...
- vue-cli 初始化项目时开发环境中的跨域问题
最近刚刚完成自己的毕业设计(基于Vue的信息资讯展示与管理平台),于是想整理一下过程遇到的一些问题. 项目基于Vue开发,使用 Vue-cli 初始化项目文件目录时默认占用8080端口,而我又想使用 ...
- 【剑指Offer面试编程题】题目1513:二进制中1的个数--九度OJ
题目描述: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 输入: 输入可能包含多个测试样例. 对于每个输入文件,第一行输入一个整数T,代表测试样例的数量.对于每个测试样例输入为一个 ...