Terrible Sets_单调栈
Description
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
Output
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_单调栈的更多相关文章
- PKU 2082 Terrible Sets(单调栈)
题目大意:原题链接 一排紧密相连的矩形,求能构成的最大矩形面积. 为了防止栈为空,所以提前加入元素(0,0). #include<cstdio> #include<stack> ...
- POJ-2081 Terrible Sets(暴力,单调栈)
Terrible Sets Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4113 Accepted: 2122 Descrip ...
- POJ 2082 Terrible Sets(单调栈)
[题目链接] http://poj.org/problem?id=2082 [题目大意] 给出一些长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题解] 我们 ...
- BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 8748 Solved: 3835[Submi ...
- BZOJ 4453: cys就是要拿英魂![后缀数组 ST表 单调栈类似物]
4453: cys就是要拿英魂! Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 90 Solved: 46[Submit][Status][Discu ...
- BZOJ 3238: [Ahoi2013]差异 [后缀数组 单调栈]
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2326 Solved: 1054[Submit][Status ...
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
- bzoj1510: [POI2006]Kra-The Disks(单调栈)
这道题可以O(n)解决,用二分还更慢一点 维护一个单调栈,模拟掉盘子的过程就行了 #include<stdio.h> #include<string.h> #include&l ...
- BZOJ1057[ZJOI2007]棋盘制作 [单调栈]
题目描述 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴阳. 而我们的 ...
随机推荐
- Text Justification [LeetCode]
Problem Description:http://oj.leetcode.com/problems/text-justification/ Note: Just be careful about ...
- JavaScript prototype 属性
prototype 属性使开发人员有能力向对象添加属性和方法. 语法 object.prototype.name=value 实例 在本例中,我们将展示如何使用 prototype 属性来向对象添加属 ...
- 腾讯大规模Hadoop集群实践 [转程序员杂志]
TDW(Tencent distributed Data Warehouse,腾讯分布式数据仓库)基于开源软件Hadoop和Hive进行构建,打破了传统数据仓库不能线性扩展.可控性差的局限,并且根据腾 ...
- 学习记录008-crond和visudo
1.每隔两个小时将/etc/servers文件打包备份到.tmp下(每次名字不同) [root@kaka cc.log]# tar zcvf /server/backup/ccc.log_$(date ...
- phonegap开发入门
做了几次开发配置了,但时间一长就忘了,特记录一下. 一.环境变量配置::右击“我的电脑”-->"高级"-->"环境变量" 1.在系统变量里新建JAV ...
- 修改PE文件的入口函数OEP
修改入口函数地址.这个是最省事的办法,在原PE文件中新增加一个节,计算新节的RVA,然后修改入口代码,使其指向新增加的节.当然,如果.text节空隙足够大的话,不用添加新节也可以. BOOL Chan ...
- nginx查看配置文件nginx.conf路径
当你执行 nginx -t 得时候,nginx会去测试你得配置文件得语法,并告诉你配置文件是否写得正确,同时也告诉了你配置文件得路径: # nginx -t nginx: the configu ...
- LibSVM使用指南
LibSVM使用指南 一. SVM简介 在进行下面的内容时我们认为你已经具备了数据挖掘的基础知识. SVM是新近出现的强大的数据挖掘工具,它在文本分类.手写文字识别.图像分类.生物序列分析等实 ...
- 继承自NSObject的类不能用CGRect
我用的是Xcode6.2. 系统默认没有pch文件. 所以没有自动导入UIKit包. 我在继承NSObject类里也不能用CGRect或者UI开头的控件,原因也是Xcode6.2以后版本 缺少UIKi ...
- 构建者模式(Builder)示例代码
package com.test; /** * Created by xiaonanhai on 2015/5/30. */ public class Builder { private String ...