poj_3250 单调栈
题目大意
N头牛排成一列,每头牛A都能看到它前面比它矮的牛i,若前面有一个比他高的牛X,则X之前的牛j,A都无法看到。给出N头牛的高度,求出这N头牛可以看到牛的数目的总数。
题目分析
画图之后,可以很容易看出是典型的单调栈问题。单调栈问题中栈元素一般有两个属性:一是牛的索引位置,二是牛的高度。每次得到一个牛index的高度height之后,都和栈顶元素进行比较,若height > stack[top].height 则直接将(新牛的高度height,新牛的索引index)入栈,否则,弹栈,直到栈顶元素stack[top].height < height,再将(新牛的高度height,新牛的索引index)入栈。
在牛A被出栈的时候,可以获得牛A能够看到的牛的个数(即新入栈的牛B的索引index_b - 牛A的索引index_a)。最后记得清空栈元素来获得所有值。
实现(c++)
可以不使用单调栈
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<set>
#include<set>
#include<map>
#include<functional>
#include<algorithm>
using namespace std;
const int kMax = 80005; //height 数组
int height[kMax]; //从i点向左看,第一个高度等于或者大于height[i]的数组下标
int higher_index[kMax]; long long int solve(int n){
int j;
long long result = 0;
for (int i = 0; i < n; i++){
j = i - 1;
while (j >= 0 && height[j] < height[i]){
j = higher_index[j];
}
higher_index[i] = j;
result += (i - j - 1);
}
return result;
}
int main(){
int n;
scanf("%d", &n);
//题目要求从左向右看,将数组颠倒,然后换个方向,从右向左看,结果是一样的
for (int i = n - 1; i >= 0; i--){
scanf("%d", &height[i]);
}
long long int result = solve(n);
printf("%lld\n", result);
return 0;
}
使用单调栈
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define MAX_COW_NUM 80005
int st[MAX_COW_NUM][2];
int gCanSeeNum[MAX_COW_NUM]; int main(){
int n;
scanf("%d", &n);
int height, top = -1;
long long sum = 0;
for (int i = 0; i < n; i++){
scanf("%d", &height);
while (top >= 0 && st[top][1] <= height){
gCanSeeNum[st[top][0]] = i - st[top][0] - 1;
sum += gCanSeeNum[st[top][0]];
top--;
}
top++;
st[top][0] = i;
st[top][1] = height;
}
while (top >= 0){
gCanSeeNum[st[top][0]] = n - st[top][0] - 1;
sum += gCanSeeNum[st[top][0]];
top--;
}
printf("%lld\n", sum);
return 0;
}
使用单调栈2
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<set>
#include<set>
#include<map>
#include<functional>
#include<algorithm>
using namespace std;
const int kMax = 80005; //height 数组
int height[kMax];
int mon_stack[kMax];
long long int solve(int n){
int top = -1;
long long result = 0;
for (int i = 0; i < n; i++){
while (top >= 0 && height[mon_stack[top]] < height[i]){
top--;
}
if (top < 0)
result += i;
else
result += (i - mon_stack[top] - 1);
mon_stack[++top] = i;
}
return result;
}
int main(){
int n;
scanf("%d", &n);
//题目要求从左向右看,将数组颠倒,然后换个方向,从右向左看,结果是一样的
for (int i = n - 1; i >= 0; i--){
scanf("%d", &height[i]);
}
long long int result = solve(n);
printf("%lld\n", result);
return 0;
}
poj_3250 单调栈的更多相关文章
- BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 8748 Solved: 3835[Submi ...
- BZOJ 4453: cys就是要拿英魂![后缀数组 ST表 单调栈类似物]
4453: cys就是要拿英魂! Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 90 Solved: 46[Submit][Status][Discu ...
- BZOJ 3238: [Ahoi2013]差异 [后缀数组 单调栈]
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2326 Solved: 1054[Submit][Status ...
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
- bzoj1510: [POI2006]Kra-The Disks(单调栈)
这道题可以O(n)解决,用二分还更慢一点 维护一个单调栈,模拟掉盘子的过程就行了 #include<stdio.h> #include<string.h> #include&l ...
- BZOJ1057[ZJOI2007]棋盘制作 [单调栈]
题目描述 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴阳. 而我们的 ...
- 洛谷U4859matrix[单调栈]
题目描述 给一个元素均为正整数的矩阵,上升矩阵的定义为矩阵中每行.每列都是严格递增的. 求给定矩阵中上升子矩阵的数量. 输入输出格式 输入格式: 第一行两个正整数n.m,表示矩阵的行数.列数. 接下来 ...
- POJ3250[USACO2006Nov]Bad Hair Day[单调栈]
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17774 Accepted: 6000 Des ...
- CodeForces 548D 单调栈
Mike and Feet Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Subm ...
随机推荐
- 利用optparse模块解析指令的字符串
optparse模块主要用来为脚本传递命令参数,采用预先定义好的选项来解析命令行参数. 使用方法: 生成OptionParser对象,为对象添加option,用parse_args方法解析文字 具体实 ...
- python --对象的属性
转自:http://www.cnblogs.com/vamei/archive/2012/12/11/2772448.html Python一切皆对象(object),每个对象都可能有多个属性(att ...
- C#:Application操作(待补充)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C#:XML操作(简单)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...
- quartusii开发过程中路径不能出现空格或中文
quartusii开发过程中路径不能出现空格或中文,否则软件出现.stf文件错误提示,开发环境搭建的时候也不能出现空格和中文,否则也会报错.
- CTC loss 理解
参考文献 CTC学习笔记(一) 简介:https://blog.csdn.net/xmdxcsj/article/details/51763868 CTC学习笔记(二) 训练和公式推导 很详细的公示推 ...
- CentOS 之 Supervisor
CentOS 之 Supervisor supervisor是一个Linux上用来管理程序后台运行的工具,支持程序的自启动,挂掉重启,日志等功能.可配置程序随系统启动,并支持挂掉重启,增强程序稳定性. ...
- java中long型时间戳的计算
计算时间的时候碰到的问题: Date d = new Date(); long currtime = d.getTime(); //获取当前时间 long starttime = currtime - ...
- Android7.0 MessageQueue
Android中的消息处理机制大量依赖于Handler.每一个Handler都有相应的Looper,用于不断地从相应的MessageQueue中取出消息处理. 一直以来,觉得MessageQueue应 ...
- Unity和安卓互调
Unity调安卓 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); And ...