POJ 2082(最大連續矩形面積)


Terrible Sets
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 2428   Accepted: 1215

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 w 1h 1+w 2h 2+...+w nh n < 10 9.

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
測試用例的情況

解題分析:

//想罵人,簡單的題意說的那么高深,特別是最后一句,估計管理員要陰笑啦
//大致題意:給定連續的矩形的寬和長,求出最大的連續矩形的面積 
/*
維護一個棧中元素高度單調遞增的棧,初始化棧中第一個元素高度寬度均為0,
然后每次讀入一個矩形,若它比棧頂元素還高就直接進棧,
否則不斷將棧中元素彈棧,直到當前棧頂元素能夠與讀入的矩形滿足高度遞增。
彈棧過程中累加彈出的元素的寬度,然后每彈出一個就判斷當前彈出元素的高度×
累加的寬度能否更新最大面積ans。然后以新的矩形作高,
已經彈出棧的元素總寬度加上新矩形寬度作寬,把這個矩形插入到棧里。
最終棧肯定是一個單調的,只需要再把棧一個個彈空,彈棧過程中仍像上面那樣計算即可。
*/
#include <iostream>
#include <cstring>
#include <stack>
using namespace std;
typedef struct Node
{
    int w,h;
}Node;
int main()
{
    int i,j,k,T;
    stack <Node > s;
    while(cin>>T,~T)
    {
        int max_area = 0;
        int total_w,cur_area;
        Node *rect = new Node[T+2];
        for(i=0;i<T;i++)
        {
            cin>>rect[i].w>>rect[i].h;
            if(s.empty())
                s.push(rect[i]);
            else
            {
                total_w=cur_area=0;
                if(rect[i].h>=s.top().h)//此處是大於等於 
                    s.push(rect[i]);
                else
                {
                    while(!s.empty())
                    {
                        if(rect[i].h<s.top().h)//此處只是小於
                        {
                            total_w += s.top().w;
                            if((cur_area=total_w*s.top().h)>max_area)
                                max_area = cur_area;
                            s.pop();
                        }
                        else
                            break;//跳出和繼續下一次是不一樣的 
                    } 
                    total_w += rect[i].w;
                    rect[i].w = total_w;
                    s.push(rect[i]);
                }
            }
        }
        total_w = cur_area = 0;
        while(!s.empty())
        {
            total_w += s.top().w;
            if((cur_area=total_w*s.top().h)>max_area)
                max_area = cur_area;  
            s.pop();         
        } 
        cout<<max_area<<endl;
        delete []rect;//加不加均AC
    }
    //system("pause");
  s.clear();//沒加也AC return 0; }

 

 

 

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM