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 ...
随机推荐
- spring in action 5.1 小结 spring mvc起步
0 配置 DispatcherServlet 是 spring mvc的核心,常规配置方法可以查看之前博客.springMVC简单例子 在此使用servlet 3 规范和 spring3.1 功能增强 ...
- 为什么会找不到D层文件?
近期两天在重装系统,今天好不easy把各种东西都装齐全了,再打开我的机房收费系统,就提演示样例如以下错误: 看到这个问题.我感觉非常熟,由于曾经也遇到过两次这个问题,都是改了下D层的编译路径.改到了U ...
- 带密匙的php加密解密代码
php加密解密示例,代码如下: <?php $id = "http://www.jbxue.com"; $token = encrypt($id, 'E', 'jbxue') ...
- Cocos2d-x 3.4 初体验——安装教程
电脑系统window7 32位 1.首先从官网下载cocos2d-x并解压 http://cn.cocos2d-x.org/download/ 解压后的文件夹中有一个setup.py,双击运行.需要安 ...
- Java 之进制转换
//十进制转十六进制 import java.util.Scanner; public class Main{ public static void main(String[] args){ Scan ...
- 认识rem
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- QT4.8.5 源码编译记录
今天想将以前的虚拟机的 QT4.8.5 集成到一个虚拟机里面,所以就重新编译了一次 QT4.8.5的源码 走了一点点小弯路,特此记录. 一.交叉编译器,不能直接从原来的虚拟机里面拷贝,必须使用官网的交 ...
- 2018-11-17 js的this引起的血案
js的this. 昨天测试,删除商品会报错,马上去测了一把,的确会报错.为毛线呢? SubOrderItem: function (orderitem) { if (orderitem.ordernu ...
- Linux系统查看公网IP地址
curl members.3322.org/dyndns/getip
- java调用kettle_实现(2)
(1).参照“java调用kettle_导入jar包(1)”,应用etl工具下lib里的所有jar (2). 最近要对一个系统的数据同步到另一个系统中,要求新系统的数据结果完成之后,实时同步到另一个系 ...