poj3250 Bad Hair Day 单调栈(递减)
Bad Hair Day
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 24420 | Accepted: 8292 |
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>
#define ll long long
#include<stack>
using namespace std;
stack<ll>p; //栈里面存的是下标
ll a[];
ll n,ans;
int main()
{
while(~scanf("%lld",&n))
{
while(!p.empty())
p.pop();
for(int i=;i<n;i++)
scanf("%lld",&a[i]);
a[n]=;//为找比a[n-1]大的数准备,因为是递减栈,将a[n]设为最大值
ans=;
for(int i=;i<=n;i++)
{
if(p.empty()||a[i]<a[p.top()])//符合严格单调递减规则,入栈
p.push(i);
else
{
while(!p.empty()&&a[i]>=a[p.top()])//找到第一个不小于栈顶元素的数的下标
{
ll top;
top=p.top();
p.pop();
ans=ans+(i-top-);//这个数到第一个不小于这个数之间的数都是比这个数小,开区间
}
//如果a[i]可以使当前栈严格单调递减,入栈
p.push(i);
}
}
printf("%lld\n",ans);
}
return ; }
poj3250 Bad Hair Day 单调栈(递减)的更多相关文章
- POJ3250[USACO2006Nov]Bad Hair Day[单调栈]
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17774 Accepted: 6000 Des ...
- Bad Hair Day [POJ3250] [单调栈 或 二分+RMQ]
题意Farmer John的奶牛在风中凌乱了它们的发型……每只奶牛都有一个身高hi(1 ≤ hi ≤ 1,000,000,000),现在在这里有一排全部面向右方的奶牛,一共有N只(1 ≤ N ≤ 80 ...
- poj3250(单调栈模板题)
题目链接:https://vjudge.net/problem/POJ-3250 题意:求序列中每个点右边第一个>=自身的点的下标. 思路:简单介绍单调栈,主要用来求向左/右第一个小于/大于自身 ...
- [poj3250]单调栈 Bad Hair Day
解题关键:将每头牛看到的牛头数总和转化为每头牛被看到的次数,然后用单调栈求解,其实做这道题的目的只是熟悉下单调栈 此题为递减栈 #include<cstdio> #include<c ...
- POJ3250(单调栈)
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17614 Accepted: 5937 Des ...
- 【POJ3250】Bad Hair Day 单调栈
题目大意:给定一个由 N 个数组成的序列,求以每个序列为基准,向右最大有多少个数字都比它小. 单调栈 单调栈中维护的是数组的下标. 单调栈在每个元素出栈时统计该出栈元素的答案贡献或对应的值. 单调栈主 ...
- 单调栈2 POJ3250 类似校内选拔I题
这个题再次证明了单调栈的力量 简单 单调栈 类似上次校内选拔消砖块 一堆牛面朝右排 给出从左到右的 问每个牛的能看到前面牛发型的个数之和 //re原因 因为在执行pop的时候没有判断empty 程序崩 ...
- BZOJ 4453: cys就是要拿英魂![后缀数组 ST表 单调栈类似物]
4453: cys就是要拿英魂! Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 90 Solved: 46[Submit][Status][Discu ...
- POJ2796Feel Good[单调栈]
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 13376 Accepted: 3719 Case T ...
随机推荐
- (全排列)Ignatius and the Princess II -- HDU -- 1027
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1027 Ignatius and the Princess II Time Limit: 2000/100 ...
- Centos 7 安装 mysql5.7
1.需要下载mysql 下载地址:http://dev.mysql.com/downloads/mysql/ 2.将下载的rpm包上传到centos 7上(我是放在根下面的opt目录) 3. 安装my ...
- MySQL—练习2
参考链接:https://www.cnblogs.com/edisonchou/p/3878135.html 感谢博主 https://blog.csdn.net/flycat296/articl ...
- linux 进程通信之 管道和FIFO
进程间通信:IPC概念 IPC:Interprocess Communication,通过内核提供的缓冲区进行数据交换的机制. IPC通信的方式: pipe:管道(最简单) fifo:有名管道 mma ...
- Javascript Object.defineProperty()
转载声明: 本文标题:Javascript Object.defineProperty() 本文链接:http://www.zuojj.com/archives/994.html,转载请注明转自Ben ...
- 开源应用框架BitAdminCore:更新日志20180817
索引 NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/coo ...
- 栈实现 C语言
最近上来写了一下栈,理解数据结构的栈. 头文件:stack.h 初始化栈结构与函数定义: #include<stdlib.h> #include <stdio.h> #incl ...
- 【ocp-12c】最新Oracle OCP-071考试题库(37题)
19.choose the best answer View the Exhibit and examine the structure of the PROMOTIONS table. Evalua ...
- “全栈2019”Java第一百一十章:局部内部类与匿名内部类区别详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- Bootstrap框架常用总结
Bootstrap框架常用标签: 标题标签:<h1>-<h6> bootstrap中也设置的相同的样式 - 若要使用 必须使用空标签来定义 比如<s ...