zoukankan      html  css  js  c++  java
  • hdu 1828 Picture 切割线求周长

    Picture

    Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2144    Accepted Submission(s): 1139


    Problem Description
    A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter.

    Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1.



    The corresponding boundary is the whole set of line segments drawn in Figure 2.



    The vertices of all rectangles have integer coordinates.
     
    Input
    Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.

    0 <= number of rectangles < 5000
    All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.

    Please process to the end of file.
     
    Output
    Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.
     
    Sample Input
     
    7
    -15 0 5 10
    -5 8 20 25
    15 -4 24 14
    0 -6 16 4
    2 15 10 22
    30 10 36 20
    34 0 40 16
     
    Sample Output
    228
     
    /*
    统计周长
    */
    
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #define N 5003
    #define hxl 10003
    using namespace std;
    
    
    struct node
    {
        int begin;
        int end;
        int val;
        int flag;
    }Linex[N<<1],Liney[N<<1];
    int Hash[30000];
    
    
    bool cmp(node n1,node n2)
    {
        return n1.val<n2.val;
    }
    
    
    int make_x(int NN)
    {
        int sum=0,i,j;
        for(i=1;i<=NN;i++)
        {
            if(Linex[i].flag==1)
            {
                for(j=Linex[i].begin;j<Linex[i].end;j++)
                {
                    if(Hash[j]==0)
                    sum++;
                    Hash[j]++;
                }
            }
            else
            {
                for(j=Linex[i].begin;j<Linex[i].end;j++)
                {
                    if(Hash[j]==1)
                    sum++;
                    Hash[j]--;
                }
    
            }
        }
        return sum;
    }
    
    int make_y(int NN)
    {
        int sum=0,i,j;
        for(i=1;i<=NN;i++)
        {
            if(Liney[i].flag==1)
            {
                for(j=Liney[i].begin;j<Liney[i].end;j++)
                {
                    if(Hash[j]==0)
                    sum++;
                    Hash[j]++;
                }
            }
            else
            {
                for(j=Liney[i].begin;j<Liney[i].end;j++)
                {
                    if(Hash[j]==1)
                    sum++;
                    Hash[j]--;
                }
            }
        }
        return sum;
    }
    
    void make_ini(int NN)
    {
        int x1,y1,x2,y2,i,len=0,sum=0;
        for(i=1;i<=NN;i++)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            x1+=hxl;y1+=hxl;y2+=hxl;x2+=hxl;
    
            Linex[++len].val=x1;
            Linex[len].begin=y1;
            Linex[len].end=y2;
            Linex[len].flag=1;
    
            Liney[len].val=y1;
            Liney[len].begin=x1;
            Liney[len].end=x2;
            Liney[len].flag=1;
    
            Linex[++len].val=x2;
            Linex[len].begin=y1;
            Linex[len].end=y2;
            Linex[len].flag=-1;
    
            Liney[len].val=y2;
            Liney[len].begin=x1;
            Liney[len].end=x2;
            Liney[len].flag=-1;
        }
        sort(Linex+1,Linex+1+len,cmp);
        sort(Liney+1,Liney+1+len,cmp);
        memset(Hash,0,sizeof(Hash));
        sum+=make_x(len);
        memset(Hash,0,sizeof(Hash));
        sum+=make_y(len);
        printf("%d
    ",sum);
    }
    
    int main()
    {
        int NN;
        while(scanf("%d",&NN)>0)
        {
            make_ini(NN);
        }
        return 0;
    }
  • 相关阅读:
    多线程--ThreadLocal类
    常用开发类库支持--UUID及空值处理Optional
    国际化的程序实现及其原理
    浅析java设计模式(一)----异构容器,可以存储任何对象类型为其他类提供该对象
    使用批处理命令注册运行mysql数据库,无需注册mysql服务,可以在任意电脑登录使用
    计算机中位、字长、字的区别
    SQL Server用户自定义数据类型
    简单的回合制小游戏
    单链表创建、删除、查找、插入之C语言实现
    LeetCode-905 Sort Array By Parity Solution (with Java)
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3234338.html
Copyright © 2011-2022 走看看