Terrible Sets

Time Limit: 1000MS Memory Limit: 30000K

Total Submissions: 4113 Accepted: 2122

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

题目说的乱七八糟看不懂,看别人的题解上面的题意才知道要干什么。这道题目有O(n)的算法就是运用单调栈,我写的暴力的方法枚举矩形,向左右扫描。

#include <iostream>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm> using namespace std;
#define MAX 50000
struct Node
{
int w;
int h;
}a[MAX+5];
int n;
int ans;
int sum;
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==-1)
break;
ans=0;
sum=0;
for(int i=0;i<n;i++)
scanf("%d%d",&a[i].w,&a[i].h);
for(int i=0;i<n;i++)
{
sum=0;
for(int j=i+1;j<n;j++)
{
if(a[j].h>=a[i].h)
sum+=a[i].h*a[j].w;
else
break;
}
for(int p=i-1;p>=0;p--)
{
if(a[p].h>=a[i].h)
sum+=a[i].h*a[p].w;
else
break;
}
sum+=a[i].w*a[i].h;
ans=max(ans,sum); }
printf("%d\n",ans);
}
return 0; }

POJ-2081 Terrible Sets(暴力,单调栈)的更多相关文章

  1. POJ 2082 Terrible Sets(单调栈)

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

  2. poj2082 Terrible Sets(单调栈)

    Description Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all re ...

  3. poj 2769 感觉♂良好 (单调栈)

    poj 2769 感觉♂良好 (单调栈) 比尔正在研发一种关于人类情感的新数学理论.他最近致力于研究一个日子的好坏,如何影响人们对某个时期的回忆. 比尔为人的一天赋予了一个正整数值. 比尔称这个值为当 ...

  4. poj 3250 Bad Hair Day (单调栈)

    http://poj.org/problem?id=3250 Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  5. POJ 2082 Terrible Sets

    Terrible Sets Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2747   Accepted: 1389 Des ...

  6. POJ 2796 Feel Good 【单调栈】

    传送门:http://poj.org/problem?id=2796 题意:给你一串数字,需要你求出(某个子区间乘以这段区间中的最小值)所得到的最大值 例子: 6 3 1 6 4 5 2 当L=3,R ...

  7. POJ 2796 Feel Good(单调栈)

    传送门 Description Bill is developing a new mathematical theory for human emotions. His recent investig ...

  8. POJ 3250 Bad Hair Day --单调栈(单调队列?)

    维护一个单调栈,保持从大到小的顺序,每次加入一个元素都将其推到尽可能栈底,知道碰到一个比他大的,然后res+=tail,说明这个cow的头可以被前面tail个cow看到.如果中间出现一个超级高的,自然 ...

  9. poj 3250 Bad Hair Day (单调栈)

    Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14883   Accepted: 4940 Des ...

  10. Feel Good POJ - 2796 (前缀和+单调栈)(详解)

    Bill is developing a new mathematical theory for human emotions. His recent investigations are dedic ...

随机推荐

  1. 【scala】 scala 映射和元组操作(四)

    1.映射  Map 定义 ,取值,遍历,排序 2. 元组 定义,取值,拉链操作 import scala.collection.mutable /** * 映射和元组 * * @author xwol ...

  2. js中forEach,for in,for of的区别

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. ios开发之--通过通知监听textfield的输入状态,判断按钮的状态

    第一步: _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _rightBtn.frame = CGRectMake(kWidth - ...

  4. 【Postgres】PostgreSQL配置远程连接

    1.开启相应的防火墙端口,缺省是5432 2.访问权限配置,D:\Program Files (x86)\PostgreSQL\9.2\data/pg_hba.conf中加入如下配置,开启远程访问 3 ...

  5. 手把手让你实现开源企业级web高并发解决方案(lvs+heartbeat+varnish+nginx+eAccelerator+memcached)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://freeze.blog.51cto.com/1846439/677348 此文凝聚 ...

  6. VMware按装ISO

    破解码 vmware12 5A02H-AU243-TZJ49-GTC7K-3C61N vmware14CG54H-D8D0H-H8DHY-C6X7X-N2KG6 创建虚拟机 也可以选第三个直接选择Ce ...

  7. make clean、make mrproper、make distclean的区别【转】

    本文转载自:http://blog.csdn.net/liyayao/article/details/6818061 内核编译时, 到底用make clean, make mrproper还是make ...

  8. Java类文件结构

    一.概述 实现语言无关性的基础是虚拟机和字节码存储格式.Java虚拟机不和包括Java在内的任何语言绑定,只与"Class文件"这种特定的二进制文件所关联,Class文件中包含了J ...

  9. 原:android4.2.2蓝牙源码阅读--bluedroid部分

    概念: GKI:统一内核接口 BTE栈: BTU栈:BTU栈开始前必须调用BTE栈初始化 代码阅读: /external/bluetooth/bluedroid/hci/:HCI library实现 ...

  10. Oralce分析函数

    1 列传行  listagg(city,',')  within GROUP (order by city)    over (partition by nation) rank with temp ...