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. iphone弹出窗口效果的制作(Core animation, CALayer)

    效果类似人人网微薄客户端的弹出效果 static CGFloat kTransitionDuration = 0.3; - (void)initView { UIWindow *window = [U ...

  2. TIMEOUT HANDLING WITH HTTPCLIENT

    https://www.thomaslevesque.com/2018/02/25/better-timeout-handling-with-httpclient/ The problem If yo ...

  3. flexbox常用布局上下固定,中间滚动

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  4. javascript全屏操作

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

  5. java的代理和动态代理简单测试

    什么叫代理与动态代理? 1.以买火车票多的生活实例说明. 因为天天调bug所以我没有时间去火车票,然后就给火车票代理商打电话订票,然后代理商就去火车站给我买票.就这么理解,需要我做的事情,代理商帮我办 ...

  6. iOS开发--打印NSRange,CGRect等结构体

    使用对应的转换NSStringFromCGPoint   NSStringFromCGSize   NSStringFromCGRect  NSStringFromCGAffineTransform  ...

  7. html主要笔记

    1.用title属性作为工具提示 2.链接到锚点 <a href="http://wickedlysmart.com/buzz#Coffee"> 3.<em> ...

  8. IOS 第三方支付的使用:支付宝

     本文转载至 http://blog.csdn.net/u014011807/article/details/47726799 总结一下支付宝iOS使用步骤: 1 第三方支付:支付宝 使用过程: 1. ...

  9. 《Lua程序设计》第3章 表达式 学习笔记

    3.1 算术操作符“+”(加法).“-”(减法).“*”(乘法).“/”(除法).“^”(指数).“%”(取模).3.2 关系运算符< > <= >= == ~=3.3 逻辑操 ...

  10. sql语句建表,并且自增加主键

    sql语句建表,并且自增加主键 use [test] CREATE TABLE [dbo].[Table_4] ( [userid] [int] IDENTITY(1,1) NOT NULL, CON ...