POJ 2082 Terrible Sets

Terrible Sets

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 5325   Accepted: 2713

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

题意:

在 x 轴上给定一些不会重叠的矩形宽和高,矩形沿着x轴对齐,求这些矩形组成的连续矩形区域的最大面积。

Sample One

题解:维护一个高度非递减的栈,如果遇到一个高度更小的矩形,则他和比他高的矩形可共用底边,而当他入队时,他的宽就是和前面可共用的宽的和,(而后面的进来的矩形一定比他高更大,所以无影响,而比他低的又可以和他共用底)注意一边弹栈一边更新ans的值

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<stack>
using namespace std;
const int maxn = 50000 + 5;
struct node{
    int h,w;
}p[maxn];
int H[maxn];
int main(){
    int n;

    ios::sync_with_stdio(false);
    while(scanf("%d",&n)==1){
        if(n == -1)break;
        int ans = 0;
        stack <node> s;
        for(int i = 1; i <= n; i++){
            cin>>p[i].w>>p[i].h;
            int nw = 0, nh =0;
            if(!s.empty() && p[i].h < s.top().h){
                while(!s.empty() && p[i].h < s.top().h){
                    nw += s.top().w;
                    nh = s.top().h;
                    ans = max(ans , nw*nh);
                    s.pop();
                }
                p[i].w += nw;
                s.push(p[i]);
            }
            else s.push(p[i]);

        }
        int nw = 0, nh = 0;
        while(!s.empty()){
            nh = s.top().h;
            nw += s.top().w;
            ans = max(ans , nw*nh);
            s.pop();
        }
        cout<<ans<<endl;
    }

}

原文地址:https://www.cnblogs.com/EdSheeran/p/8459824.html

时间: 2024-10-10 04:37:13

POJ 2082 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

stack(单调栈) POJ 2082 Terrible Sets

题目传送门 题意:紧贴x轴有一些挨着的矩形,给出每个矩形的长宽,问能组成的最大矩形面积为多少 分析:用堆栈来维护高度递增的矩形,遇到高度小的,弹出顶部矩形直到符合递增,顺便计算矩形面积,且将弹出的宽度都累积到当前的矩形中,这样最后再扫描一遍,算面积很方便,这题应该算是 POJ 2559 的强化版了 收获:stack的应用,求矩形面积,矩阵相乘,表达式计算 代码: /************************************************ * Author :Running

Terrible Sets

Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3017   Accepted: 1561 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

POJ2082 Terrible Sets

Terrible Sets Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5067   Accepted: 2593 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 element

POJ 2082 题解

Terrible Sets Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5330   Accepted: 2715 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 element

计算最大矩形面积,POJ(2082)

题目链接:http://poj.org/problem?id=2082 把矩形按照高度一次递增的循序排列,当违反这一规则的时候,更新ans,用新的data替换之前的矩形.然后最后扫一遍. #include <iostream> #include <stack> #include <cstdio> using namespace std; struct rec { int w; ///宽 int h; ///高 } data; int main() { int n; wh

Terrible Sets(求矩形的最大面积(递增栈))

题意:紧贴x轴有一些相互挨着的矩形,给定每个矩形的长宽,问他们可以形成的最大矩形是多少. Sample Input 3 1 2 3 4 1 2 3 3 4 1 2 3 4 -1 Sample Output 12 14 1 #include <iostream> 2 #include <stack> 3 #include <cstdio> 4 5 using namespace std; 6 7 struct rec{ 8 int w; 9 int h; 10 11 }d

poj 动态规划题目列表及总结

此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276,1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740(博弈),1742, 1887, 1926(马尔科夫矩阵,求平衡), 1936, 1952, 1953, 1958, 1959, 1962, 1975,

[转] POJ DP问题

列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740, 1742, 1887, 1926, 1936, 1952, 1953, 1958, 1959, 1962, 1975, 1989, 2018, 2029, 2039, 2063, 20