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的更多相关文章

  1. POJ 2559 Program C

    Submit Status Practice POJ 2559 Description A histogram is a polygon composed of a sequence of recta ...

  2. [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)

    [POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...

  3. poj 2559 Largest Rectangle in a Histogram 栈

    // poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...

  4. stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram

    题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...

  5. 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 ...

  6. poj 2559 Largest Rectangle in a Histogram (单调栈)

    http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 6 ...

  7. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    [题目链接] http://poj.org/problem?id=2559 [题目大意] 给出一些宽度为1的长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题 ...

  8. 【POJ 2559】 Largest Rectangle in a Histogram

    [题目链接] http://poj.org/problem?id=2559 [算法] 单调栈 [代码] #include <algorithm> #include <bitset&g ...

  9. 题解 POJ 2559【Largest Rectangle in a Histogram】(单调栈)

    题目链接:http://poj.org/problem?id=2559 思路:单调栈 什么是单调栈? 单调栈,顾名思义,就是单调的栈,也就是占中存的东西永远是单调(也就是递增或递减)的 如何实现一个单 ...

随机推荐

  1. 普通浏览器GET请求与Ajax的GET请求的区别

    看图 普通浏览器请求 Ajax请求

  2. pch

    #define kWeakSelf(weakSelf) __weak __typeof(self)weakSelf = self; #ifndef __OPTIMIZE__#define NSLog( ...

  3. 深入分析,理解jQuery.Deferred源码

    前言: 如果你对jQuery.Callback回调对象不了解,或者只掌握其方法,但是没有通过阅读源码理解,可以先阅读 前一章jQuery.Callbacks源码解读二,因为只有完全理解jQuery.C ...

  4. ELK常见错误分析(转)

    ELK 常见错误处理   ELK 这里就不介绍了,如何安装请参考博客之前的文章.在这里感谢ttlsa团队,同时,我很荣幸能加入到ttlsa团队中,分享点滴,凉白开说发文章有红包,期待这篇群主能给多少红 ...

  5. R语言 常见模型

    转自 雪晴网 [R]如何确定最适合数据集的机器学习算法 抽查(Spot checking)机器学习算法是指如何找出最适合于给定数据集的算法模型.本文中我将介绍八个常用于抽查的机器学习算法,文中还包括各 ...

  6. 关于Sublime text 的PHP编译环境配置的问题

    前一段时间终于装上了传说中的代码编辑神器====>Sublime Text ,一打开便爱不释手,于是在网上找PHP的配置方案和插件,所有的一切都搞定了,可就是编译的时候没有显示,也没有提示,熬了 ...

  7. PHP的$_SERVER['PHP_SELF']造成的XSS漏洞攻击及其解决方案

    $_SERVER['PHP_SELF']简介 $_SERVER['PHP_SELF'] 表示当前 PHP文件相对于网站根目录的位置地址,与 document root 相关. 假设我们有如下网址,$_ ...

  8. linux 驱动程序中断号的申请

    1.linux下查看硬中断与软中断 硬中断:/proc/interrupts 软中断:/proc/softirqs 往往一些硬件中断号都是跟CPU所分配的硬件中断号相匹配的. 即同类型的cpu可能对同 ...

  9. git ssh-add 报错 ssh-add Could not open a connection to your authentication agent

    $ ssh-add ~/.ssh/id_rsa.pub Could not open a connection to your authentication agent. 启动ssh-agent服务 ...

  10. BZOJ3277——串

    0.题意:给定你n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串(注意包括本身). 1.分析:这个题我问了吴大爷做法 首先建立后缀自动机,然后利用离线搞出每一个 ...