zoukankan      html  css  js  c++  java
  • hdu 1543 Paint the Wall

    题目大意:一块高为h,宽为m的墙,在墙上涂上不同颜色的矩形,求经过一系列操作后每种颜色相应的面积和颜色种数。需要注意的是输出时要判断颜色种数,如果是1或0要用is,否则用are(这里害我wa了好几次)。-_-!

    思路:离散化。

    View Code
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 using namespace std;
     5 int a[205][205];
     6 struct 
     7 {
     8     int x,y;
     9 }bottomleft[105],topright[105];
    10 int col[205];
    11 int xx[205];
    12 int yy[205];
    13 int area[105];
    14 int binsearch(int kk[],int l,int r,int key)
    15 {
    16     int m=(l+r)>>1;
    17     if(kk[m]==key)
    18     return m;
    19     if(kk[m]>key)
    20     return binsearch(kk,l,m-1,key);
    21     return binsearch(kk,m+1,r,key);
    22 }
    23 void paint(int l1,int l2,int r1,int r2,int c)
    24 {
    25     int i,j;
    26     for(i=r1;i<r2;i++)
    27     for(j=l1;j<l2;j++)
    28     a[i][j]=c;
    29 }
    30 void cal(int k1,int k2)
    31 {
    32     int i,j;
    33     for(i=1;i<k2;i++)
    34     for(j=1;j<k1;j++)
    35     if(a[i][j])
    36         area[a[i][j]]+=(xx[j+1]-xx[j])*(yy[i+1]-yy[i]);
    37 }
    38 int main()
    39 {
    40     int h,w,cas=1;
    41     while(scanf("%d%d",&h,&w)&&(h||w)){
    42         int n,i,k1=1,k2=1;
    43         scanf("%d",&n);
    44         for(i=0;i<n;i++){
    45             scanf("%d%d%d%d%d",&bottomleft[i].x,&bottomleft[i].y,&topright[i].x,&topright[i].y,col+i);
    46             xx[k1++]=bottomleft[i].x;
    47             xx[k1++]=topright[i].x;
    48             yy[k2++]=bottomleft[i].y;
    49             yy[k2++]=topright[i].y;
    50         }
    51         if(cas!=1)
    52         printf("\n");
    53         sort(xx+1,xx+k1);
    54         sort(yy+1,yy+k2);
    55         int m=2;
    56         for(i=2;i<k1;i++)
    57         if(xx[i]!=xx[i-1])
    58         xx[m++]=xx[i];
    59         k1=m;
    60         m=2;
    61         for(i=2;i<k2;i++)
    62         if(yy[i]!=yy[i-1])
    63         yy[m++]=yy[i];
    64         k2=m;
    65         memset(a,0,sizeof(a));
    66         memset(area,0,sizeof(area));
    67         for(i=0;i<n;i++){
    68             int l1=binsearch(xx,1,k1-1,bottomleft[i].x);
    69             int l2=binsearch(xx,1,k1-1,topright[i].x);
    70             int r1=binsearch(yy,1,k2-1,bottomleft[i].y);
    71             int r2=binsearch(yy,1,k2-1,topright[i].y);
    72             paint(l1,l2,r1,r2,col[i]);
    73         }
    74         cal(k1,k2);
    75         int cnt=0;
    76         printf("Case %d:\n",cas++);
    77         for(i=1;i<101;i++)
    78         if(area[i]){
    79         printf("%d %d\n",i,area[i]);
    80         cnt++;
    81         }
    82         if(cnt<=1)
    83         printf("There is %d color left on the wall.\n",cnt);
    84         else
    85         printf("There are %d colors left on the wall.\n",cnt);
    86     }
    87 }
  • 相关阅读:
    视觉slam十四讲课后习题ch3-7
    视觉slam十四讲课后习题ch3-6
    视觉slam十四讲课后习题ch3--5题
    (原创)遗传算法C++实现
    (笔记):组合and继承之访问限制(二)
    (笔记):组合and继承之访问限制(一)
    (笔记):初始化列表之初始化顺序
    (笔记):构造函数之初始化列表
    笔记:构造函数易错点
    (笔记):构造函数与析构函数
  • 原文地址:https://www.cnblogs.com/kim888168/p/2761668.html
Copyright © 2011-2022 走看看