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. 【如何快速的开发一个完整的iOS直播app】(推流篇)

    前言 在看这篇之前,如果您还不了解直播原理,请查看这篇文章如何快速的开发一个完整的iOS直播app(原理篇) 开发一款直播app,肯定需要流媒体服务器,本篇主要讲解直播中流媒体服务器搭建,并且讲解了如 ...

  2. 课堂练习&课下作业

    设计思路: 列举出买十本的所有情况:1.一本的时候不打折扣 2.两本的时候买两本价最低 3.三本的时候买三本价最低 4.四本的时候买四本价最低 5.五本的时候买五本价最低 6.六本的时候分一本和五本价 ...

  3. nyoj-746

    整数划分(四) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 暑假来了,hrdv 又要留学校在参加ACM集训了,集训的生活非常Happy(ps:你懂得),可是他最近 ...

  4. arrow css

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Android SDK的安装以及环境变量的配置

    想来试试app的开发,于是便需要在电脑上安装各种软件包,其中Android SDK就是其中一种,特地记录一下其安装过程: 1.下载地址http://baoku.360.cn/soft/show/app ...

  6. 开源软件架构总结之——Bash(readline做输入交互式,词法语法分析,进程交互)

    第3章 The Bourne-Again Shell Bash的主要组件:输入处理,解析,单词展开(word expansion)和其他命令处理,管道(pipeline)中的命令执行.这些组件构成一个 ...

  7. Js笔试题之正则表达式

    一.复习字符串的传统操作 如何获取一个字符串中的数字字符,并按数组形式输出,如 dgfhfgh254bhku289fgdhdy675gfh 输出[254,289,675] 分析:循环用charAt() ...

  8. (DFS)hdoj1198-Farm Irrigation

    题目链接 DFS的简单应用,比较繁琐的是处理输入的英文字母.用并查集也可以做(可是笔者现在还没有掌握并查集,之前只用过一次,以后学会回来补上) #include<cstdio> #incl ...

  9. Hibernate中的多对多映射

    1.需求 项目与开发员工 一个项目,有多个开发人员 一个开发人员,参与多个项目 [多对多] 2.实体bean设计 Project: public class Project { private int ...

  10. bzoj 1036 Tree Count

    题目大意:给出一棵树,每个点有一个权值,要求三种操作:1.修改某个点的权值,2.询问x到y路径上各点的权值最大值,3.询问x到y路径上各点的权值之和. #include <cstdio> ...