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-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 题意:
给定N个面向右边的奶牛,我们说一只奶牛a[i],能看到另外一个牛奶的头发当且仅当 有一只奶牛a[j]瞒住,a[j]<a[i],并且j>i 思路:
裸的单调栈,维护一个从栈顶到栈底,严格不减小的栈。 细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rt return
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll n;
ll a[maxn];
ll r[maxn];
int main()
{
gbtb;
cin>>n;
repd(i,,n)
{
cin>>a[i];
}
stack<int> s;
for(int i=n;i>=;i--)
{
while(s.size()&&a[s.top()]<a[i])
{
s.pop();
}
if(s.size())
{
r[i]=s.top();
}else
{
r[i]=n+;
}
s.push(i);
} // repd(i,1,n)
// {
// cout<<r[i]<<" ";
// }
// cout<<endl; ll ans=0ll;
repd(i,,n)
{
ans+=1ll*(r[i]-i-);
// db(r[i]-i-1);
}
cout<<ans<<endl;
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Bad Hair Day POJ - 3250 (单调栈入门题)的更多相关文章
- Poj 3250 单调栈
1.Poj 3250 Bad Hair Day 2.链接:http://poj.org/problem?id=3250 3.总结:单调栈 题意:n头牛,当i>j,j在i的右边并且i与j之间的所 ...
- BZOJ1113 海报PLA1(单调栈入门题)
一,自己思考下 1,先自己思考下 N个矩形,排成一排,现在希望用尽量少的海报去cover住它们. 2,不懂. 着实不懂. 3,分析下,最优性问题对吧,然后就每什么想法了.. 虽然肯定和单调栈和单调队列 ...
- POJ 3250 Bad Hair Day【单调栈入门】
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24112 Accepted: 8208 Des ...
- hdu1506 直方图中最大的矩形 单调栈入门
hdu1506 直方图中最大的矩形 单调栈入门 直方图是由在公共基线对齐的矩形序列组成的多边形.矩形具有相同的宽度,但可能具有不同的高度.例如,左侧的数字显示了由高度为2,1,4,5,1,3,3的矩形 ...
- [Usaco2006 Mar]Mooo 奶牛的歌声(单调栈裸题)
1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 961 Solved: 679[Submi ...
- Sliding Window POJ - 2823 单调队列模板题
Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...
- poj 2059 单调栈
题意:求柱状图中最大矩形面积. 单调栈:顾名思义就是栈内元素单调递增的栈. 每次插入数据来维护这个栈,假设当前须要插入的数据小于栈顶的元素,那就一直弹出栈顶的元素.直到满足当前须要插入的元素大于栈顶元 ...
- [poj 2796]单调栈
题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include ...
- uva 1619 - Feel Good || poj 2796 单调栈
1619 - Feel Good Time limit: 3.000 seconds Bill is developing a new mathematical theory for human ...
随机推荐
- java开发基础知识学习
java环境配置 classpath: .当前目录 path: java 命令所在目录 jdk安装目录 jdk/bin jre安装目录 jre/bin 安装JDK后配置环境变量如下: 安装过程用到了j ...
- BM:EOS的创造者
2018年6月EOS的主网即将上线,EOS到底是全球骗局,还是技术创? EOS币到底能涨到几何,现在还适合不适合入手...我们暂且不说.先了解一下EOS的创造者BM,以及BM的传奇经历. BM BM是 ...
- c/c++拷贝构造函数和关键字explicit
c/c++拷贝构造函数和关键字explicit 关键字explicit 修饰构造方法的关键字,加上了,就告诉编译器,不可以隐式初始化对象:不加就可以隐式初始化对象: 下面的代码是可以正常编译执行的,但 ...
- VS快捷键失效问题
VS作为宇宙最强IDE,为我们提供了强大的快捷键组合,熟练的使用这些快捷键能极大提高我们的编码效率,但是在我们实际使用的过程中经常会遇到某个快捷键组合失效的问题. 问题原因: 一般都是VS的快捷键与电 ...
- Xmanager power suit 6 最新版注册激活
Xmanager Power Suit 6.0.0012 最新版注册激活,长期更新 操作步骤 Xmanger Power Suit 官方 其实有两种 .exe 文件,一个是用于试用的,在注册的时候不能 ...
- MATLAB简易画图
给定一组特殊点,连线作图 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 以成绩隶属函数为例: score.m cj_x1=[ 0.1]; cj_y1= ...
- [福大软工] Z班 团队Alpha阶段成绩汇总
团队成绩汇总表 团队 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 总分 Dipper 9 85 90 26 42 27.5 120 74 25 111 19 628.5 SW ...
- python3编写网络爬虫20-pyspider框架的使用
二.pyspider框架的使用 简介 pyspider是由国人binux 编写的强大的网络爬虫系统 github地址 : https://github.com/binux/pyspider 官方文档 ...
- 【大数据技术】Sqoop
1.Sqoop是什么 Sqoop:SQL-to-Hadoop,传统数据库与Hadoop间数据同步工具.(MySQL.Oracle <==> HDFS.HBase.Hive) Sqoop ...
- hdfs fsck命令查看HDFS文件对应的文件块信息(Block)和位置信息(Locations)
关键字:hdfs fsck.block.locations 在HDFS中,提供了fsck命令,用于检查HDFS上文件和目录的健康状态.获取文件的block信息和位置信息等. fsck命令必须由HDFS ...