题目链接: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!=-) {
int ans=;
int totalw=; ///总宽
int curarea=; ///当前面积
stack<rec> s; ///定义一个长方形的栈
int lasth=; ///上一次进栈的矩形的高度
for(int i=; i<n; i++) {
scanf("%d%d",&data.w,&data.h);
if(data.h>=lasth) {
s.push(data);
} else {
///更新ans
totalw=;
curarea=;
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=; ///总宽度
curarea=; ///当前面积
while(!s.empty()) {
totalw+=s.top().w;
curarea=totalw*s.top().h;
if(curarea>ans)
ans=curarea;
s.pop();
}
printf("%d\n",ans);
}
return ;
}

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

  1. POJ 1151 Atlantis(线段树-扫描线,矩形面积并)

    题目链接:http://poj.org/problem?id=1151 题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积 题目思路:矩形面积并. 代码如下: #include<stdio ...

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

    题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线 ...

  3. POJ 1151 Atlantis 线段树求矩形面积并 方法详解

    第一次做线段树扫描法的题,网搜各种讲解,发现大多数都讲得太过简洁,不是太容易理解.所以自己打算写一个详细的.看完必会o(∩_∩)o 顾名思义,扫描法就是用一根想象中的线扫过所有矩形,在写代码的过程中, ...

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

    题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...

  5. poj 2559 最大矩形面积(单调栈)

    题目:输入一个整数n,代表有n个  1(宽度) * h[i](高度)的矩形.接下来n个数依次给定一个矩形高度的高度h[i](i<=n). 求:在给定的依次排列的这堆矩形构成的图形里用一个矩形圈出 ...

  6. hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  7. C++编程计算图形的面积(圆、矩形)

    C++基础,while循环与if判断实现的计算图形面积 1 #include <iostream> 2 3 int main() { 4 while (true){ 5 int input ...

  8. POJ2599+POJ2082【最大矩形面积】

    题目链接:http://poj.org/problem?id=2559 题目链接:http://poj.org/problem?id=2082 这一类题目的解法,不知自己闲着没事就做了两个. 果然压栈 ...

  9. stack(单调栈) POJ 2082 Terrible Sets

    题目传送门 题意:紧贴x轴有一些挨着的矩形,给出每个矩形的长宽,问能组成的最大矩形面积为多少 分析:用堆栈来维护高度递增的矩形,遇到高度小的,弹出顶部矩形直到符合递增,顺便计算矩形面积,且将弹出的宽度 ...

随机推荐

  1. 4 练习: 使用eclipse开发

    1       练习: 使用eclipse开发 1.1  练习目标 本例讲述在使用eclipse如何创建groovy程序. 1.2  创建new Groovy project 本例假设你已经安装好了g ...

  2. 问题:eclipse中线程编程编译报错,undefined reference to 'pthread_create'的解决方法(已解决)

    问题描述: 在Ubuntu系统中,使用eclipse CDT集成开发环境编写pthread程序,编译时,pthread_create不通过,报错信息是: undefined reference to ...

  3. How to Fold a Julia Fractal

    How to Fold a Julia FractalA tale of numbers that like to turn http://acko.net/blog/how-to-fold-a-ju ...

  4. [转]AngularJS 使用 UI Router 实现表单向导

    本文转自:http://www.oschina.net/translate/angularjs-multi-step-form-using-ui-router 今天我们将使用AngularJs和伟大的 ...

  5. ZK典型应用场景

    1. 数据发布/订阅--动态获取数据 2.Master选举 a). 利用Zk会保证无法重复创建一个已经存在的节点 b). 多个客户端同时创建,创建成功的即是master,并监控master节点,一旦m ...

  6. 如何实现Nginx+Keepalived中Nginx进程的高可用

    此架构我简单说明下: 一般为了维护方便,企业网站的服务器都在自己的内部机房里,只开放了Keepalived的VIP地址的两个端口80.443,通过Juniper SSG550防火墙映射出去,外网DNS ...

  7. linux下追查线上问题常用命令

    (1)查占用cpu最多的进程方法一:核心指令:ps实际命令:ps H -eo pid,pcpu | sort -nk2 | tail执行效果如下:[work@test01 ~]$ ps H -eo p ...

  8. List转为DataTable并可以导出为Excel

    using com.jd120.Core.Utility; using System; using System.Collections.Generic; using System.Data; usi ...

  9. Spring Boot 2.x 自定义metrics 并导出到influxdb

    Step 1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  10. [LeetCode]17. Letter Combinations of a Phone Number电话号码的字母组合

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...