HDU 2478 Slides(n个矩形中n-1个矩形相交最大面积)

题目链接:
HDU 2478 Slides
题意:
给出n个矩形的左下角和右上角坐标,求出n-1个矩形相交的最大面积。
分析:
先找到n个矩形左下角中的最右上角和次右上角的x,y坐标(Left_fir.x, Left_fir.y, Left_sec.x, Left_sec.y),右上角中的最左下角和次左下角的x,y坐标(Right_fir.x, Right_fir.y, Right_sec.x, Right_sec.y)。
其实左下角中的最小右上角x,y和右上角中的最左下角x,y就是n个矩形相交出来的矩形。
然后可以枚举每个矩形,如果第i个矩形的左下角的横坐标是Left_fir.x,那么如果去掉这个矩形,剩下的n个矩形相交的左下角的横坐标是Left_sec.x,否则相交矩形的左下角的横坐标是Left_fir.x,左下角的纵坐标和右上角坐标类似。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N = 100010;
const int INF = 0x3f3f3f3f;

int T, n;
struct Point{
    int x, y;
}Left[MAX_N], Right[MAX_N], Left_fir, Left_sec, Right_fir, Right_sec;

int main()
{
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        Left_fir.x = Left_fir.y = Left_sec.x = Left_sec.y = -INF;
        Right_fir.x = Right_fir.y = Right_sec.x = Right_sec.y = INF;
        for(int i = 0; i < n; ++i){
            scanf("%d%d%d%d", &Left[i].x, &Left[i].y, &Right[i].x, &Right[i].y);
            if(Left[i].x > Left_fir.x){
                Left_sec.x = Left_fir.x;
                Left_fir.x = Left[i].x;
            }else if(Left[i].x > Left_sec.x){
                Left_sec.x = Left[i].x;
            }

            if(Left[i].y > Left_fir.y){
                Left_sec.y = Left_fir.y;
                Left_fir.y = Left[i].y;
            }else if(Left[i].y > Left_sec.y){
                Left_sec.y = Left[i].y;
            }

            if(Right[i].x < Right_fir.x){
                Right_sec.x = Right_fir.x;
                Right_fir.x = Right[i].x;
            }else if(Right[i].x < Right_sec.x){
                Right_sec.x = Right[i].x;
            }
            if(Right[i].y < Right_fir.y){
                Right_sec.y = Right_fir.y;
                Right_fir.y = Right[i].y;
            }else if(Right[i].y < Right_sec.y){
                Right_sec.y = Right[i].y;
            }
        }
        //printf("left_fir:(%d, %d) left_sec:(%d, %d)\n", Left_fir.x, Left_fir.y, Left_sec.x, Left_sec.y);
        //printf("right_fir:(%d %d) right_sec:(%d, %d)\n", Right_fir.x, Right_fir.y, Right_sec.x, Right_sec.y);
        int ans = 0;
        Point tmp_left, tmp_right;
        for(int i = 0; i < n; ++i){
            if(Left[i].x != Left_fir.x) tmp_left.x = Left_fir.x;
            else tmp_left.x = Left_sec.x;
            if(Left[i].y != Left_fir.y) tmp_left.y = Left_fir.y;
            else tmp_left.y = Left_sec.y;

            if(Right[i].x != Right_fir.x) tmp_right.x = Right_fir.x;
            else tmp_right.x = Right_sec.x;
            if(Right[i].y != Right_fir.y) tmp_right.y = Right_fir.y;
            else tmp_right.y = Right_sec.y;

            //printf("tmp_left:(%d, %d) tmp_right:(%d, %d)\n", tmp_left.x, tmp_left.y, tmp_right.x, tmp_right.y);
            if(tmp_left.x < tmp_right.x && tmp_left.y < tmp_right.y){
                ans = max(ans, (tmp_right.y - tmp_left.y) * (tmp_right.x - tmp_left.x));
            }
            //printf("i = %d ans = %d\n", i, ans);
        }
        printf("%d\n", ans);
    }
    return 0;
}

你可能感兴趣的:(HDU,Thinking,More)