离散化 Paint the Wall ZOJ - 2747

题目

题意:有一个w*h的墙,在墙上涂色n次,后面涂色会把前面同个位置的颜色覆盖掉,问n次涂色之后,墙面上有几个颜色,每种颜色分别有多少块。

思路:因为 0 <= n <= 100,但是 0 <= w,h <= 10000;如果每次都每一个格子遍历过去,那么时间c差不多10000*10000*100肯定要超时了,但是我们发现n很小,所以我们可以用离散化。把x坐标y坐标离散化,再遍历一下就可以了。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn = 2e2+10;
struct Node{
    int x1, y1, x2, y2, c;
}node[maxn];

int Map[maxn][maxn], x[maxn], y[maxn];

int main(){
    int w, h, n, Case = 1;;
    while(scanf("%d%d",&w,&h)!=EOF){
        if(w == 0|| h == 0)
            break;
        memset(x, 0, sizeof(x));
        memset(y, 0, sizeof(y));
        memset(Map, 0, sizeof(Map));
        scanf("%d",&n);
        int n1 = 0, n2 = 0;
        for(int i = 1; i <= n; i++){
            scanf("%d%d%d%d%d",&node[i].x1,&node[i].y1,&node[i].x2,&node[i].y2,&node[i].c);
            x[n1++] = node[i].x1;
            y[n2++] = node[i].y1;
            x[n1++] = node[i].x2;
            y[n2++] = node[i].y2;
        }
        sort(x, x + n1);      //离散化
        sort(y, y + n2);
        int lx = unique(x, x+n1) - x;
        int ly = unique(y, y+n2) - y;
        for(int i = 1; i <= n; i++){
            int xl = lower_bound(x, x + lx, node[i].x1) - x;
            int xr = lower_bound(x, x + lx, node[i].x2) - x;
            int yl = lower_bound(y, y + ly, node[i].y1) - y;
            int yr = lower_bound(y, y + ly, node[i].y2) - y;

            for(int j = xl; j < xr; j++){
                for(int k = yl; k < yr; k++){
                    Map[j][k] = node[i].c;
                }
            }
        }
        int color[maxn];
        memset(color, 0, sizeof(color));
        int ans = 0;
        for(int i = 0; i < lx; i++){
            for(int j = 0; j < ly; j++){
                if(Map[i][j])
                    color[Map[i][j]] += (x[i+1] - x[i]) * (y[j+1] - y[j]);
            }
        }
        if(Case > 1)
            printf("\n");
        printf("Case %d:\n",Case++);
        int num = 0;
        for(int i = 1; i <= 100; i++){
            if(color[i]){
                printf("%d %d\n",i, color[i]);
                num++;
            }
        }
        if(num <= 1){
            printf("There is %d color left on the wall.\n", num);
        }
        else
            printf("There are %d colors left on the wall.\n",num);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值