POJ 2559
http://poj.org/problem?id=2559
题意:就是找出可以完整连接的最大的矩形面积。
思路:找出单独的一块矩形,往两边延伸,记录两边的比他高的矩形是在哪个位置,然后最右的位置减去最左边的矩形的位置。就是这个矩形最大可构成的面积。
但是,如果一个一个用循环去做的话,结果是必定超时的,所以这里要用到单调栈。
比如找出最左边的比目标矩形要高的矩形的位置的代码
while(!s.empty()) //对栈首先进行清空。 s.pop(); s.push(); //入栈一个边界位置。 for(int i=;i<=n;i++){ for(x=s.top();a[x]>=a[i];x=s.top()) //如果a[x]要比那个a[i]
也就是目标矩形要大的话,那么说明可以继续往左寻找。如果没有比目标矩形要大的话,那么这个就是
它的右边那个就是最临界的那个矩形。
s.pop(); l[i]=x+; s.push(i);
}
#include <stdio.h>
#include <iostream>
#include <stack> #define X 1000010 using namespace std; stack<int >s;
int n,x;
long long a[X],m,ans,r[X],l[X];
int main()
{
// freopen("in.txt","r",stdin);
while(scanf("%d",&n),n!=){
ans=;
a[]=-;a[n+]=-;
for(int i=;i<=n;i++)
scanf("%lld",&a[i]);
while(!s.empty())
s.pop(); s.push();
for(int i=;i<=n;i++){
for(x=s.top();a[x]>=a[i];x=s.top())
s.pop();
l[i]=x+;
s.push(i);
}
while(!s.empty())
s.pop();s.push(n+);
for(int i=n;i>;i--){
for(x=s.top();a[x]>=a[i];x=s.top())
s.pop();
r[i]=x-;
s.push(i);
if((r[i]-l[i]+)*a[i]>ans) ans=(r[i]-l[i]+)*a[i];
}
printf("%lld\n",ans);
}
return ;
}
POJ 2559的更多相关文章
- POJ 2559 Program C
Submit Status Practice POJ 2559 Description A histogram is a polygon composed of a sequence of recta ...
- [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)
[POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...
- poj 2559 Largest Rectangle in a Histogram 栈
// poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...
- stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram
题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...
- POJ 2559 Largest Rectangle in a Histogram -- 动态规划
题目地址:http://poj.org/problem?id=2559 Description A histogram is a polygon composed of a sequence of r ...
- poj 2559 Largest Rectangle in a Histogram (单调栈)
http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 6 ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
[题目链接] http://poj.org/problem?id=2559 [题目大意] 给出一些宽度为1的长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题 ...
- 【POJ 2559】 Largest Rectangle in a Histogram
[题目链接] http://poj.org/problem?id=2559 [算法] 单调栈 [代码] #include <algorithm> #include <bitset&g ...
- 题解 POJ 2559【Largest Rectangle in a Histogram】(单调栈)
题目链接:http://poj.org/problem?id=2559 思路:单调栈 什么是单调栈? 单调栈,顾名思义,就是单调的栈,也就是占中存的东西永远是单调(也就是递增或递减)的 如何实现一个单 ...
随机推荐
- centos 7 DenyHosts 安装 防暴力破解ssh登陆
为了减少软件扫描ssh登陆 还是用这个比较好点 默认端口号22 也要改 登陆密码也不要使用 弱口令 123456 这样的 Description DenyHosts is a python prog ...
- string.replace正则表达式说明
str.replace(reg,function($0,$1,$2...,index,str){ }); $0: 匹配模式的字符串$1...: 匹配模式子表达式的字符串,0个或多个,个数取决于子表达式 ...
- 使用CFURLCreateStringByAddingPercentEscapes进行URL编码
iOS程序访问HTTP资源时需要对URL进行UTF8编码,特酷吧在之前一直都喜欢使用NSString的stringByAddingPercentEscapesUsingEncoding方法进行编码.今 ...
- Jenkins入门总结
Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括: 1.持续的软件版本发布/测试项目. 2.监控外部调用执行的工作 在网上貌似没有找到Jenkins的中文的太多的文 ...
- motto11
我们应该这样来提高自己表达能力:在和人交流的时候,以欣赏的态度接受对方的观点,如果不太同意对方的观点,不能说对方的观点不好,而应该说,你的想法(观点)很好,但我认为,xxxxxx这样做会更好些. 这样 ...
- thinkphp-许愿墙-3
用jquery写异步传递的时候, 首先要判断表单中的输入是否为空: 如果有多个输入项, 应该, 分别的, 一步一步的来判断是否为空, 而不是用 and / or来复合判断! 同时如果为空, 应该将它设 ...
- linux shell 报错 Syntax error: Bad for loop variable
在linux下写了一个简单的shell,循环10次. test.sh #!/bin/bash ## ##循环10次 ## ; i<; i++)); do echo Good Morning ,t ...
- 了不起的Node.js读书笔记
原文摘自我的前端博客,欢迎大家来访问 http://www.hacke2.cn 第二章 Js概览 基于GoogleV8引擎 Object.keys(o) 数组方法:遍历forEach.过滤filter ...
- 迷你版Deferred
直接贴代码: /** * 迷你版的deferred */ function Deferred(func) { if (this instanceof Deferred === false) { ret ...
- jQuery 2.0发布,不再支持IE6/7/8
有时发现jQuery库引用的都对,javascript代码写的也没问题,可是jquery就是出现问题,额--我发现换个jquery库就没问题了,长时间不关注jquery的问题而已: 很多人都没有使用最 ...