Terrible Sets_单调栈

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=50005;
int n;
struct node
{
    int w,h;
}a[N];
int main()
{
    while(scanf("%d",&n))
    {
        int ans=0;
        if(n==-1) break;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i].w,&a[i].h);
        }
        for(int i=1;i<=n;i++)
        {
            int sum=0;
            for(int j=i;j>=1;j--)
            {
                if(a[j].h>=a[i].h)
                   sum+=a[j].w;
                else break;
            }
            for(int j=i+1;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 0;
}

豪华版(单调栈):

#include<iostream>
#include<stack>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=50005;
int n;
struct node
{
    int h,w;
}st[N];
int cnt;
int main()
{
    while(scanf("%d",&n))
    {
        if(n==-1) break;
        int ans=0;
        int h,w;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&w,&h);
            if(h>=st[cnt].h)
            {
                st[++cnt].w=w;
                st[cnt].h=h;
            }
            else
            {
                int sum=0;
                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=0;
        while(cnt>0)//清空栈
        {
            sum+=st[cnt].w;
            ans=max(ans,sum*st[cnt].h);
            cnt--;
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-17 18:50:20

Terrible Sets_单调栈的相关文章

poj 2082 Terrible Sets (数据结构 ——栈 STL)

 Terrible Sets Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2999   Accepted: 1549 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 elem

POJ2082---Terrible Sets(单调栈)

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 &

(单调栈)poj-2559 Largest Rectangle in a Histogram

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the

【单调栈】hdu1506 Largest Rectangle in a Histogram

单调栈的介绍及一些基本性质 http://blog.csdn.net/liujian20150808/article/details/50752861 依次把矩形塞进单调栈,保持其单增,矩形中的元素是一个三元组,存储其位置,高度,以及以其为高度的情况下,大矩形的左边界最多扩展到哪里. 每次将新的元素塞进栈的时候,其左边界就是其左侧第一个小于它的矩形的位置+1. 然后,每个矩形出栈的时候,记录其右边界为当前往栈里面塞的矩形的位置-1,然后更新答案即可. 注意最后把所有的矩形出栈,更新答案. #in

BZOJ 3238 AHOI 2013 差异 后缀数组+单调栈

题目大意: 思路:一看各种后缀那就是后缀数组没跑了. 求出sa,height之后就可以乱搞了.对于height数组中的一个值,height[i]来说,这个值能够作为lcp值的作用域只在左边第一个比他小的位置到右边第一个比他小的位置.这个东西很明显可以倍增RMQ+二分/单调栈. 之后就是数学题了 Σlen[Ti] + len[Tj] = (len + 1) * len * (len - 1),之后吧所有求出来的Σ2 * lcp(Ti,Tj)减掉就是答案. 记得答案开long long CODE:

51nod 1215 数组的宽度&amp;poj 2796 Feel Good(单调栈)

单调栈求每个数在哪些区间是最值的经典操作. 把数一个一个丢进单调栈,弹出的时候[st[top-1]+1,i-1]这段区间就是弹出的数为最值的区间. poj2796 弹出的时候更新答案即可 #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> #include<cmath

[poj3250]单调栈 Bad Hair Day

解题关键:将每头牛看到的牛头数总和转化为每头牛被看到的次数,然后用单调栈求解,其实做这道题的目的只是熟悉下单调栈 此题为递减栈 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<stack> 6 #include<iostream> 7 using namespace std; 8 typedef lo

HDU 5033---Building(单调栈)

题目链接 Problem Description Once upon a time Matt went to a small town. The town was so small and narrow that he can regard the town as a pivot. There were some skyscrapers in the town, each located at position xi with its height hi. All skyscrapers loc

POJ 3415 Common Substrings(后缀数组+单调栈)

[题目链接] http://poj.org/problem?id=3415 [题目大意] 求出两个字符串长度大于k的公共子串的数目. [题解] 首先,很容易想到O(n2)的算法,将A串和B串加拼接符相连, 做一遍后缀数组,把分别属于A和B的所有后缀匹配,LCP-k+1就是对答案的贡献, 但是在这个基础上该如何优化呢. 我们可以发现按照sa的顺序下来,每个后缀和前面的串的LCP就是区间LCP的最小值, 那么我们维护一个单调栈,将所有单调递减的LCP值合并, 保存数量和长度,对每个属于B串的后缀更新