zoukankan      html  css  js  c++  java
  • Problem : [Usaco2017 Dec]Blocked Billboard

    Problem : [Usaco2017 Dec]Blocked Billboard

    Time Limit: 1 Sec Memory Limit: 128 MB

    Description

    奶牛Bessie喜欢看两块巨大的奶牛饲料品广告牌,她觉得这比自己农场的牧草更加美味。不过有一天广告牌前停着一辆巨大的卡车,挡住了广告牌,Bessie想知道她现在还能看到多少广告牌
    坐标的范围在[-1000,1000]之间,保证广告牌之间没有重叠区域

    Input

    第一行四个整数,x1,y1,x2,y2,(x1,y1),(x2,y2)分别为第一块广告牌的左下角和右上角的坐标;第二行类似,制定第二块广告牌的坐标;第三行指定卡车的坐标
    Output
    一行一个整数,表示未被遮挡的广告牌的面积之和

    Sample Input

    1 2 3 5
    6 0 10 4
    2 1 8 3

    Sample Output

    17
    HINT

    在这个问题中,我们有两个可能被第三个矩形覆盖的矩形,我们希望计算出在第三个矩形区域之外的任意一个矩形内的总面积。

    因为原始的矩形是保证不重叠的,我们可以考虑更简单的问题,即需要在一个矩形内而在另一个矩形外计算答案。如果我们知道如何做,那么我们可以独立地计算两个矩形的这个数量,并返回和。

    我们可以通过计算第一个矩形的面积,然后减去两个矩形相交处的面积,来计算一个矩形内而另一个矩形外的面积。

    我们现在需要弄清楚,如何计算两个矩形相交的面积。让矩形的角分别为x1y1(x_1,y_1)x2y2(x_2,y_2)x3y3(x_3,y_3)x4y4(x_4,y_4)。如果一个点xy(x,y)在两个矩形内,则x1xx2x_1le xle x_2x3xx4x_3le xle x_4y1yy2y_1le y le y_2y3yyy4y_3le y le y le y_4。因此,这些矩形的交叉点都是xx点,其中maxx1x3lexminx2x4max(x_1,x_3)、le xlemin(x_2,x_4)maxy1y3leyminy2y4max(y_1,y_3)、le ylemin(y_2,y_4)点,如果存在任何此类点,保证为矩形。

    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    struct Point{
        int x,y;
        void Read(){
            scanf("%d%d",&y,&x);
            return;
        }
    };
    struct Retangle{
        Point lu,rd;    //lu是左上角坐标,rd是右下角坐标
        void Read(){
            lu.Read();
            rd.Read();
    
            return;
        }
    
        int Area(){
            return (rd.x-lu.x)*(rd.y-lu.y);
        }
    }cpp,txt,ad;
    bool In(Retangle a,Retangle b){
        return bool(a.lu.x>=b.lu.x and a.rd.x<=b.rd.x and a.lu.y>=b.lu.y and a.rd.y<=b.rd.y) ;
    }
    int X[10],Y[10];
    int main(int argc, char const *argv[]){
        cpp.Read();
        txt.Read();
        ad.Read();
        X[1]=cpp.lu.x,Y[1]=cpp.lu.y;
        X[2]=cpp.rd.x,Y[2]=cpp.rd.y;
        X[3]=txt.lu.x,Y[3]=txt.lu.y;
        X[4]=txt.rd.x,Y[4]=txt.rd.y;
        X[5]=ad.lu.x,Y[5]=ad.lu.y;
        X[6]=ad.rd.x,Y[6]=ad.rd.y;
        sort(X+1,X+6+1);
        sort(Y+1,Y+6+1);
        int ans=0;
        for(int i=1;i<6;i++)
            for(int j=1;j<6;j++){
                Retangle R=(Retangle){ (Point){X[i],Y[j]},(Point){X[i+1],Y[j+1]} };
                if( !In(R,ad) and ( In(R,cpp) or In(R,txt) ) )
                    ans+=R.Area();
            }
        printf("%d",ans);
        return 0;
    }
    
  • 相关阅读:
    Generate Parentheses
    Length of Last Word
    Maximum Subarray
    Count and Say
    二分搜索算法
    Search Insert Position
    Implement strStr()
    Remove Element
    Remove Duplicates from Sorted Array
    Remove Nth Node From End of List
  • 原文地址:https://www.cnblogs.com/ZhaoChongyan/p/11740423.html
Copyright © 2011-2022 走看看