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. ASP.NET Padding Oracle Attack EXP

    #!/usr/bin/perl## PadBuster v0.3 - Automated script for performing Padding Oracle attacks# Brian Hol ...

  2. idea配置2个tomcat

    复制tomcat   分别放在不同地方

  3. R语言画图实例-参考R语言实战

    dose <- c(, , , ,) drugA <- c(, , , , ) drugB <- c(, , , , ) # 数据准备 opar <- par(no.reado ...

  4. C语言动态内存分配

    考虑下面三段代码: 片段1 void GetMemory(char *p) { p = (); } void Test(void) { char *str = NULL; GetMemory(str) ...

  5. [译]Create a Web API in MVC 6

    原文: http://www.asp.net/vnext/overview/aspnet-vnext/create-a-web-api-with-mvc-6 ASP.NET 5.0的一个目标是合并MV ...

  6. IOC和bean容器

  7. KVM虚拟机内存不足,调整参数

    Dec :: vgfs001 kernel: tiotest_AMD_x86 invoked oom-killer: gfp_mask=, oom_adj=, oom_score_adj= Dec : ...

  8. 【bzoj3631】[JLOI2014]松鼠的新家

    题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在"树"上.松 ...

  9. JSON格式转换(javascript)

    使用ajax从后台抓取数据后,如果有多个值,可以使用json传值. ajax例子如下,在返回的类型里面,可以是文本型(text),JSON格式(json),超文本类型(html),XML文件类型(xm ...

  10. 基于SSL协议的双向认证 - 双向认证 [3]

    1      SSL双向认证的实现 这里是基于SSL和Tomcat配置实现的,配置方法如下: 1.1    生成CA数字证书 首先需要配置OPENSSL环境变量. 我的OPENSSL配置文件路径是“D ...