Description

Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all real numbers. wi, hi for i = 1 . . . n are some elements in N, and w0 = 0. 
Define set B = {< x, y > | x, y ∈ R and there exists an index i > 0 such that 0 <= y <= hi ,∑0<=j<=i-1wj <= x <= ∑0<=j<=iwj} 
Again, define set S = {A| A = WH for some W , H ∈ R+ and there exists x0, y0 in N such that the set T = { < x , y > | x, y ∈ R and x0 <= x <= x0 +W and y0 <= y <= y0 + H} is contained in set B}. 
Your mission now. What is Max(S)? 
Wow, it looks like a terrible problem. Problems that appear to be terrible are sometimes actually easy. 
But for this one, believe me, it's difficult.

Input

The input consists of several test cases. For each case, n is given in a single line, and then followed by n lines, each containing wi and hi separated by a single space. The last line of the input is an single integer -1, indicating the end of input. You may assume that 1 <= n <= 50000 and w1h1+w2h2+...+wnhn < 109.

Output

Simply output Max(S) in a single line for each case.

Sample Input

3
1 2
3 4
1 2
3
3 4
1 2
3 4
-1

Sample Output

12
14

【题意】给出一些小矩形的长、宽;求最大的矩形面积。

普通版:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=;
int n;
struct node
{
int w,h;
}a[N];
int main()
{
while(scanf("%d",&n))
{
int ans=;
if(n==-) break;
for(int i=;i<=n;i++)
{
scanf("%d%d",&a[i].w,&a[i].h);
}
for(int i=;i<=n;i++)
{
int sum=;
for(int j=i;j>=;j--)
{
if(a[j].h>=a[i].h)
sum+=a[j].w;
else break;
}
for(int j=i+;j<=n;j++)
{
if(a[j].h>=a[i].h)
sum+=a[j].w;
else break;
}
ans=max(ans,sum*a[i].h);
}
printf("%d\n",ans);
}
return ;
}

豪华版(单调栈):

#include<iostream>
#include<stack>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=;
int n;
struct node
{
int h,w;
}st[N];
int cnt;
int main()
{
while(scanf("%d",&n))
{
if(n==-) break;
int ans=;
int h,w;
for(int i=;i<=n;i++)
{
scanf("%d%d",&w,&h);
if(h>=st[cnt].h)
{
st[++cnt].w=w;
st[cnt].h=h;
}
else
{
int sum=;
while(st[cnt].h>=h)
{
sum+=st[cnt].w;
ans=max(ans,sum*st[cnt].h);
cnt--;
}
sum+=w;
st[++cnt].w=sum;
st[cnt].h=h;
}
}
int sum=;
while(cnt>)//清空栈
{
sum+=st[cnt].w;
ans=max(ans,sum*st[cnt].h);
cnt--;
}
printf("%d\n",ans);
}
return ;
}

Terrible Sets_单调栈的更多相关文章

  1. PKU 2082 Terrible Sets(单调栈)

    题目大意:原题链接 一排紧密相连的矩形,求能构成的最大矩形面积. 为了防止栈为空,所以提前加入元素(0,0). #include<cstdio> #include<stack> ...

  2. POJ-2081 Terrible Sets(暴力,单调栈)

    Terrible Sets Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4113 Accepted: 2122 Descrip ...

  3. POJ 2082 Terrible Sets(单调栈)

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

  4. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

  5. BZOJ 4453: cys就是要拿英魂![后缀数组 ST表 单调栈类似物]

    4453: cys就是要拿英魂! Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 90  Solved: 46[Submit][Status][Discu ...

  6. BZOJ 3238: [Ahoi2013]差异 [后缀数组 单调栈]

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2326  Solved: 1054[Submit][Status ...

  7. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  8. bzoj1510: [POI2006]Kra-The Disks(单调栈)

    这道题可以O(n)解决,用二分还更慢一点 维护一个单调栈,模拟掉盘子的过程就行了 #include<stdio.h> #include<string.h> #include&l ...

  9. BZOJ1057[ZJOI2007]棋盘制作 [单调栈]

    题目描述 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴阳. 而我们的 ...

随机推荐

  1. Xcode 8 打包上线 iTunes Connect 找不到构建版本

    Xcode 8 打包上线 iTunes Connect 找不到构建版本 最近苹果推出新的mac操作系统(macOS Sierra 10.12),大家可能都已经升级了,作为一个开发者,小编肯定是第一时间 ...

  2. ie6 ie7 绝对定位 相对定位 层被遮住

    relative 上加上z-index有多个,则z-index递减, absolute层显示则不会被其它标签挡住.

  3. win7_oracle11g_64位连接32位PLSQL_Developer

      工具/原料 已经装好的64位Oracle数据库 window7_64位的操作系统 PLSQL_Developer 9.0以上版本(目前只有32位的):下面有下载连接! 官方的 instantcli ...

  4. Greenplum——升级的分布式PostgresSQL

    Greenplum数据库基于PostgreSQL开源技术.本质上讲,它是多个PostgreSQL实例一起充当一个数据库管理系统.Greenplum以PostgreSQL 8.2.15为基础构建,在SQ ...

  5. asp.net core StaticFiles中间件修改wwwroot

    new StaticFileOptions() { FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentD ...

  6. linux 设置windows共享

    在linux下需要设置windows共享. 我们使用SMBA来实现. 1.查看是否安装smba. rpm -qa | grep samba 如果没有安装,则去 www.rpmfind.net 下载sm ...

  7. java反射之Constructor简单应用

    Constructor类是java反射中重要的类,它是对类中构造器的描述的类.类似于Method(对类中方法的描述的类),Field(对类中属性的描述的类). 通过创建Constructor的对象实例 ...

  8. Mongodb Management Studio

    1.服务器管理功能添加服务器,删除服务器 2.服务器,数据库,表,列,索引,树形显示和状态信息查看 3.查询分析器功能.支持select,insert,Delete,update支持自定义分页函数 $ ...

  9. js的严谨模式

    一.怎么用 <script type="text/javascript"> "use strict";     //放在脚本文件第一行,整个脚本将以 ...

  10. lucene4.7学习总结

    花了一段时间学习lucene今天有时间把所学的写下来,网上有很多文章但大部分都是2.X和3.X版本的(当前最新版本4.9),希望这篇文章对自己和初学者有所帮助. 学习目录 (1)什么是lucene ( ...