HDU 2478 Slides(瞎搞,預處理)

畫船聽雨發表於2014-04-24

題目的大意就是:給你n對點,第一個點表示正方形左上角的點,第二個點表示正方形右上角的點。然後從這n個點中,取走一個點,之後求出這n-1個正方形的交集所組成的矩形的面積的最大。

思路是:給出n個點之後相交的那個矩形的四個點是可以確定的。然後列舉去掉這n個點,然後判斷一下去掉這個點之後這個交集的四個點是否在這個正方形上。如果在相交的矩形的範圍就擴大到第二小的點上了啊。一次判斷四個點,最後求出面積。

Slides

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 928    Accepted Submission(s): 312


Problem Description
There are N slides lying on the table. Each of them is transparent and formed as a rectangle. In a traditional problem, one may have to calculate the intersecting area of these N slides. The definition of intersection area is such area which belongs to all of the slides.
But this time I want to take out some one of the N slides, so that the intersecting area of the left N-1 slides should be maximal. Tell me the maximum answer.
 

Input
The first line of the input contains a single integer T, the number of test cases, followed by the input data for each test case. The first line of each test case contains a single integer N (1 <= N <= 100000), the number of rectangles. Followed by N lines, each line contains four integers x1, y1, x2, y2 (-10000 <= x1 < x2 <= 10000, -10000 <= y1 < y2 <= 10000), pair (x1, y1) gives out the bottom-left corner and pair (x2, y2) gives out the top-right corner of the rectangle.
 

Output
There should be one line per test case containing the maximum intersecting area of corresponding N-1 slides.
 

Sample Input
2 2 0 0 2 2 1 1 2 2 3 0 0 2 2 1 0 3 2 1 1 3 3
 

Sample Output
4 2
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898

const int maxn = 100010;
using namespace std;

int x1[maxn], Y1[maxn], x2[maxn], y2[maxn];

int main()
{
    int T, n;
    while(~scanf("%d",&T))
    {
        while(T--)
        {
            int l1 = -INF;
            int l2 = -INF;
            int r1 = INF;
            int r2 = INF;
            int ll1 = -INF;
            int ll2 = -INF;
            int rr1 = INF;
            int rr2 = INF;
            scanf("%d",&n);
            for(int i = 0; i < n; i++)
            {
                scanf("%d %d %d %d",&x1[i], &Y1[i], &x2[i], &y2[i]);
                if(l1 < x1[i])
                {
                    l2 = l1;
                    l1 = x1[i];
                }
                else if(l2 < x1[i])
                    l2 = x1[i];

                if(r1 > x2[i])
                {
                    r2 = r1;
                    r1 = x2[i];
                }
                else if(r2 > x2[i])
                    r2 = x2[i];

                if(ll1 < Y1[i])
                {
                    ll2 = ll1;
                    ll1 = Y1[i];
                }
                else if(ll2 < Y1[i])
                    ll2 = Y1[i];

                if(rr1 > y2[i])
                {
                    rr2 = rr1;
                    rr1 = y2[i];
                }
                else if(rr2 > y2[i])
                    rr2 = y2[i];
            }
            if(n == 1)
            {
                printf("0\n");
                continue;
            }
            int xx1, xx2, yy1, yy2;
            int cnt = 0;
            for(int i = 0; i < n; i++)
            {
                if(x1[i] == l1)
                    xx1 = l2;
                else
                    xx1 = l1;

                if(x2[i] == r1)
                    xx2 = r2;
                else
                    xx2 = r1;

                if(Y1[i] == ll1)
                    yy1 = ll2;
                else
                    yy1 = ll1;

                if(y2[i] == rr1)
                    yy2 = rr2;
                else
                    yy2 = rr1;
                if(xx1 < xx2 && yy1 < yy2)
                    cnt = max(cnt, ((xx2-xx1)*(yy2-yy1)));
            }
            printf("%d\n",cnt);
        }
    }
    return 0;
}


相關文章