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.性质: 单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递 ...
随机推荐
- Write-up-NODE-1
关于 下载地址:点我 哔哩哔哩:哔哩哔哩 一天研究OBS终于不闪屏了 顺便在这里记录一下,上网查了很久.刚刚开始是不闪屏了,但是锁屏后就唤醒不了了,只能强制关机. 然后又上网找了很久,重启了N次,终于 ...
- Java程序生成exe可执行文件
Java程序打包成exe可执行文件,分为两大步骤. 第一步:将Java程序通过Eclipse或者Myeclipse导成Jar包 第二步:通过exe4j讲Jar包程序生成exe可执行文件 第一步详解: ...
- Numpy 为运算
Numpy “bitwise_” 开头的函数是位运算函数: Numpy 位运算包括以下几个函数: 函数 描述 bitwise_and 对数组元素执行位与操作 bitwise_or 对数组元素执行 ...
- 使用MyCat实现MySQL读写分离
说明 配置MyCat读写分类前需要先配置MySQL的主从复制,参考我上一篇的文章,已经做了比较详细地讲解了. 环境 centos7.MySQL5.7.mycat1.6 配置MyCat账号密码和数据库名 ...
- pytorch张量数据索引切片与维度变换操作大全(非常全)
(1-1)pytorch张量数据的索引与切片操作1.对于张量数据的索引操作主要有以下几种方式:a=torch.rand(4,3,28,28):DIM=4的张量数据a(1)a[:2]:取第一个维度的前2 ...
- 【剑指Offer面试编程题】题目1517:链表中倒数第k个结点--九度OJ
题目描述: 输入一个链表,输出该链表中倒数第k个结点. (hint: 请务必使用链表.) 输入: 输入可能包含多个测试样例,输入以EOF结束. 对于每个测试案例,输入的第一行为两个整数n和k(0< ...
- quartz spring 实现动态定时任务
在实际项目应用中经常会用到定时任务,可以通过quartz和spring的简单配置即可完成,但如果要改变任务的执行时间.频率,废弃任务等就需要改变配置甚至代码需要重启服务器,这里介绍一下如何通过quar ...
- [*CTF2019]babyflash
用JPEXS反编译flash.swf得到441张黑白图片和1个mp3文件 软件下载地址:https://github.com/jindrapetrik/jpexs-decompiler/release ...
- vagrant的使用介绍
()添加镜像到本地仓库 vagrant box add bt_centos6.6_zouke centos-6.6-x86_64.box )初始化 vagrant init ()启动vm vagran ...
- Python—处理Excel表格
一.使用xlrd和xlwt这两个库来处理excel,即xlrd是读excel的库,xlwt是写excel的库 1.使用 xlrd 读取Excel数据 # -*- coding:utf-8 -*- im ...