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. hdu---(2604)Queuing(矩阵快速幂)

    Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. XX宝面试题——JS部分

    1.sessionStorage .localStorage 和 cookie 之间的差别 sessionStorage 和 localStorage 是HTML5 Web Storage API 供 ...

  3. Java 容器(集合)

    import java.util.*; 一.基础概念 1.什么是容器? 2.为什么需要容器? 3.容器的分类? 二.重点知识 List接口 Collections类 (1)为什么需要? Collect ...

  4. 前端面试题之nina分享

    HTML相关 1.<!DOCTYPE>标签的定义与用法. <!DOCTYPE>的定义: <!DOCTYPE>声明位于文档中的最前面的位置,处于<html> ...

  5. uva 1220

    1220 - Party at Hali-Bula Time limit: 3.000 seconds Dear Contestant, I'm going to have a party at my ...

  6. Objective-C(NSString、BOOL、多文件开发)

    NSString 表示oc当中的字符串类 %@是oc当中对象的格式符 printf不能打印oc当中的对象 通过stringWithFormat:这个类方法,打印格式化的字符串 例 int a = 10 ...

  7. [转]无IDE时编译和运行Java

    本文由 ImportNew - Grey 翻译自 dzone.欢迎加入Java小组.转载请参见文章末尾的要求. 最近 Java subreddit 出现了一篇”在没有IDE的情况下编译Java包” 的 ...

  8. BZOJ1570 [JSOI2008]Blue Mary的旅行

    建分层图,每一层表示一天的情况 从S向第0层的1号点连边,每层的n向T连INF的边 枚举天数,每多一天就多建一层然后跑最大流,如果当前流量大于人数则输出答案 由于路径长度不会超过n,因此tot个人走这 ...

  9. 字符集与字符编码 (charset & encoding)

    乱码是个大坑,相信每个人都遇过,而且是个绕不过去的坑.我理解每个程序员都应该写一篇编码相关的博文,梳理自己对这一块的理解,下面是我反复理解多次之后的学习小结. 1.从记事本的不同编码说起: 打开记事本 ...

  10. scikit-learn 机器学习工具包

    scikit-learn官网:http://scikit-learn.org/stable/ scikit-learn初始化学习:  http://scikit-learn.org/stable/in ...