计算最大矩形面积,POJ(2082)

题目链接:http://poj.org/problem?id=2082

把矩形按照高度一次递增的循序排列,当违反这一规则的时候,更新ans,用新的data替换之前的矩形。然后最后扫一遍。

#include <iostream>
#include <stack>
#include <cstdio>

using namespace std;

struct rec {
    int w;      ///宽
    int h;      ///高
} data;

int main() {
    int n;
    while(scanf("%d",&n),n!=-1) {
        int ans=0;
        int totalw=0;   ///总宽
        int curarea=0;  ///当前面积
        stack<rec> s;   ///定义一个长方形的栈
        int lasth=0;    ///上一次进栈的矩形的高度
        for(int i=0; i<n; i++) {
            scanf("%d%d",&data.w,&data.h);
            if(data.h>=lasth) {
                s.push(data);
            } else {
                ///更新ans
                totalw=0;
                curarea=0;
                while(!s.empty()&&s.top().h>data.h) {
                    totalw+=s.top().w;
                    curarea=totalw*s.top().h;
                    if(curarea>ans)
                        ans=curarea;
                    s.pop();    ///抛掉,最后用新的data代替
                }
                totalw+=data.w;
                data.w=totalw;
                s.push(data);
            }
            lasth=data.h;
        }

        totalw=0;   ///总宽度
        curarea=0;  ///当前面积
        while(!s.empty()) {
            totalw+=s.top().w;
            curarea=totalw*s.top().h;
            if(curarea>ans)
                ans=curarea;
            s.pop();
        }
        printf("%d\n",ans);
    }
    return 0;
}

时间: 2024-08-05 06:15:36

计算最大矩形面积,POJ(2082)的相关文章

解题报告:LeetCode Largest Rectangle in Histogram(计算最大矩形面积)

题目出处:https://leetcode.com/problems/largest-rectangle-in-histogram/题意描述:给定n个非负的整数,代表n个依次相邻的宽度为1的柱形的高,求这些柱形所能形成的最大的矩形面积. 解决思路:此题最直接最原始的做法就是扫描起点和终点,并随时更新最大面积,但是这样的做法的复杂度为O(n^2),显然会超时,这里就不再贴代码了. 于是我们需要考虑怎么将复杂度降下来,一种想法是在求面积之前进行预处理,将每个整数左右的第一个比当前位置矮的柱形的下标l

hdu 1505 City Game 最大矩形面积 单调队列

City Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5106    Accepted Submission(s): 2197 Problem Description Bob is a strategy game programming specialist. In his new city building game t

计算直方图中最大矩形面积

题目是计算直方图中的最大矩形面积,下面是我的做法,我在网上也看到有人说可以通过栈的方式来解决,因为时间问题,并没有马上尝试,下回有时间在尝试下吧!! 还有这题有变式:计算矩阵中最大的矩形面积,其中矩阵中元素只能为1和0,代码下次补发吧!! 代码如下: #include<iostream>using namespace std; int maxSquare(const int pos,const int n,const int height[]){ if(n==1) return height[

Poj 2559 Largest Rectangle in a Histogram(柱形统计图中的最大矩形面积)

 给出一个柱形统计图中,求其中的最大矩形面积 做完这道题,搜了一下题解大部分基本都是单调栈......然而做之前并不知道这是什么,其实用递推也可以做这道题,理解起来比较容易. 用两个数组l,r记录当前坐标可以向左和向右延伸的最远位置的坐标,然后就是递推了. 初始时将l[i],r[i]的值置为i,即自己的坐标.这里拿l[i]举例: 从左向右扫描统计图,计算当前位置的l[i]时,如果h[i] > h[ l[i] - 1 ]的话,那么l[i] = l[ l[i]-1  ]. 然后对于每个位置,an

poj 3277 City Horizon (线段树 扫描线 矩形面积并)

题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不会出错, 所有的区间都能列出来,只是在查找的时候费点事. 给的矩形相当于在同一水平线上的,也就是y1坐标相当于为0,其他的就和 poj 1151 Atlantis 差不多了. 我写的思路是按照矩形面积并的思路写的: 但是还有另一种方法也是挺简单的,就是把给的矩形按照高从小到大排序,然后依次插入线段树

POJ&#183;1151 Atlantis&#183;线段树求矩形面积并

题目在这:http://poj.org/problem?id=1151 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the is

poj 1151 Atlantis(矩形面积并)

题意:每组给出矩形左上角和右下角坐标,求矩形面积并: 思路:沿水平方向计算面积并:(切成水平条): #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; const int maxn=500; struct node{ double x; int l,r,t; //t为上下边标志

POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并

题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线段树维护两个值:cover和len,cover表示该线段区间目前被覆盖的线段数目,len表示当前已覆盖的线段长度(化为离散前的真值),每次加入一条线段,将其y_low,y_high之间的区间染上line[i].cover,再以tree[1].len乘以接下来的线段的x坐标减去当前x坐标,即计算了一部

编写矩形类 计算矩形面积

public class juxing { int a; int b; juxing(int a,int b) { System.out.println("矩形面积s=" + (a*b)); } } public class juxing1 { public static void main(String[] args) { juxing s = new juxing(5, 6); } }