您的位置:首页 > 大数据 > 人工智能

hdu-1543 Paint the Wall

2016-03-22 00:01 204 查看

Paint the Wall

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 977    Accepted Submission(s): 317


[align=left]Problem Description[/align]
Here comes our future artist. See, he is painting again, this time on home's white wall, using different color of paint. Let me see, red, yellow, black, green... but why does he just paint rectangles? Pretty guy, seems he is fond
of it.

So, after he's done his great job, the white wall has been filled with so many blocks of color. Of course, some color previously painted has been covered by some color painted later. Now, the little guy has some doubt that how many different colors have been
left on the wall, and what are the areas of them. As a seven-year-old boy, he has just learned painting in kindergarten, math seems too difficult for him. So he turns to you, a college student good at math and programming, to help him figuring it out. Don't
make him disappointed.

 

[align=left]Input[/align]
Input consists of multiple test cases, each describing "a great job" done by out little guy.

Each case begins with a line containing two integers, Height and Width, the size of the wall. The next line contains an integer N, which is the number of rectangles that have been painted. N lines follow, describing the rectangles in the order they were painted.
Each line contains five integers, Top, Left, Bottom, Right, and Color, giving out the position, size and color information of the rectangle.

The range of Height and Width is [1, 10000]. There will be at least 1, and at most 100 rectangles to be painted. For each rectangle, Top and Bottom is in the range [0, Height], Left and Right is in the range [0, Width]. Bottom is strictly greater than Top,
and Right is strictly greater than Left. Color will be in the range [1, 100].

The top-left coordinate of the wall is (0, 0), and the bottom-right coordinate of the wall is (Height, Width), as shown below.
(0,0)      (0,W)
---------------
|             |
|             |
|             |
|             |
|             |
---------------
(H,0)      (H,W)


The last case is followed by a line containing two zeroes.

There is a blank line between two test cases.

 

[align=left]Output[/align]
For each painting, first output "Case X:" in a single line where X is the case number starting form 1. Then output the colors left and their areas, one color per line, in the order of color numbers (increasing). For each color, you
should output the color number, a blank space, and the area of this color on the wall. After that, you should output a single line "There is M color left on the wall." or "There are M colors left on the wall.", depending on M, which is the number of colors
left on the wall.

Output a blank line between two test cases.

 

[align=left]Sample Input[/align]

10 5
1
1 1 2 2 2

4 4
2
0 0 3 3 1
2 2 4 4 2

0 0

 

[align=left]Sample Output[/align]

Case 1:
2 1
There is 1 color left on the wall.

Case 2:
1 8
2 4
There are 2 colors left on the wall.

题意:给你一面墙,长为H,宽为W,然后给你N个矩形。矩形用两个对角给出,每个矩形颜色随机,1~100代表100种不同的颜色。一开始以为是线段树,死活没写出来。或许线段树也可以,不过参考了网上的代码。用离散化+二分写了。#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 111
int m[N<<1][N<<1];
int xs,ys;
int color[N<<1];
int x[N<<1],y[N<<1];
int findx(int a)
{
    int l=1,r=xs,mid;
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(x[mid]==a)
            return  mid;
        else if(x[mid]>a)
            r=mid-1;
        else
            l=mid+1;
    }
}
int findy(int a)
{
    int l=1,r=ys,mid;
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(y[mid]==a)
            return  mid;
        else if(y[mid]>a)
            r=mid-1;
        else
            l=mid+1;
    }
}
int main()
{
    int h,w;
    int x1
,y1
,x2
,y2
,col
;
    int c=1;
    while(scanf("%d %d",&h,&w)!=EOF)
    {
        memset(color,0,sizeof(color));
        int t=1;
        if(h==0&&w==0)
            break;
        int n;
        scanf("%d",&n);
        if(c!=1)
            printf("\n");
        for(int i=0;i<n;i++)
        {
            scanf("%d %d %d %d %d",&x1[i],&y1[i],&x2[i],&y2[i],&col[i]);
            x[t]=x1[i];
            y[t++]=y1[i];
            x[t]=x2[i];
            y[t++]=y2[i];
        }
        sort(x+1,x+t);
        sort(y+1,y+t);
        xs=1,ys=1;
        for(int i=2;i<t;i++)
        {
            if(x[i]!=x[i-1])
            {
                x[++xs]=x[i];
            }
        }
        for(int i=2;i<t;i++)
        {
            if(y[i]!=y[i-1])
            {
                y[++ys]=y[i];
            }
        }
        int sx,sy,ex,ey;
        memset(m,0,sizeof(m));
        for(int i=0;i<n;i++)
        {
            sx=findx(x1[i]);
            sy=findy(y1[i]);
            ex=findx(x2[i]);
            ey=findy(y2[i]);
            for(int j=sx;j<ex;j++)
            {
                for(int k=sy;k<ey;k++)
                {
                    m[j][k]=col[i];
                }
            }
        }
        for(int i=1;i<xs;i++)
            {
               for(int k=1;k<ys;k++)
               {
                   color[m[i][k]]+=(x[i+1]-x[i])*(y[k+1]-y[k]);
               }
            }
            printf("Case %d:\n",c++);
            int ans=0;
            for(int i=1;i<=100;i++)
            {
                if(color[i]!=0)
                {
                    printf("%d %d\n",i,color[i]);
                    ans++;
                }
            }
            if(ans<=1)
                printf("There is %d color left on the wall.\n",ans);
            else
                printf("There are %d colors left on the wall.\n",ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: