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

hdu 1543 Paint the Wall

2014-03-07 16:47 351 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1543

#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 10000
using namespace std;

int h,w,n;
int X[maxn],Y[maxn],m[500][500],clo[maxn];
struct node
{
int x1,y1,x2,y2,c;
}p[maxn*4];

int bs(int key,int l,int r,int a[])
{
int low=l,high=r;
while(low<=high)
{
int mid=(low+high)>>1;
if(a[mid]==key)
{
return mid;
}
if(a[mid]<key)
low=mid+1;
else if(a[mid]>key)
high=mid-1;
}
}
int main()
{
int case1=0;
while(scanf("%d%d",&h,&w)!=EOF)
{
if(h==0&&w==0) break;
scanf("%d",&n);
int x1,y1,x2,y2,c;
int t=0;
memset(X,0,sizeof(X));
memset(Y,0,sizeof(Y));
for(int i=0; i<n; i++)
{
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&c);
if(x1>x2) swap(x1,x2);
if(y1>y2) swap(y1,y2);
p[i].x1=x1;p[i].x2=x2;p[i].y1=y1;p[i].y2=y2;p[i].c=c;
X[t]=x1;Y[t++]=y1;
X[t]=x2;Y[t++]=y2;
}
sort(X,X+t);
sort(Y,Y+t);
int t1=1,t2=1;
memset(m,0,sizeof(m));
for(int i=1; i<t; i++) if(X[i]!=X[i-1]) X[t1++]=X[i];
for(int i=1; i<t; i++) if(Y[i]!=Y[i-1]) Y[t2++]=Y[i];
for(int i=0; i<n; i++)
{
int xx1=bs(p[i].x1,0,t1-1,X);
int yy1=bs(p[i].y1,0,t2-1,Y);
int xx2=bs(p[i].x2,0,t1-1,X);
int yy2=bs(p[i].y2,0,t2-1,Y);
for(int j=xx1; j<xx2; j++)
{
for(int k=yy1; k<yy2; k++)
{
m[j][k]=p[i].c;
}
}
}
memset(clo,0,sizeof(clo));
for(int i=0; i<t1; i++)
{
for(int j=0; j<t2; j++)
{
if(m[i][j])
{
clo[m[i][j]]+=(X[i+1]-X[i])*(Y[j+1]-Y[j]);
}
}
}
int t3=0;
if(case1) printf("\n");
printf("Case %d:\n",++case1);
for(int i=1; i<=100; i++)
{
if(clo[i]) {t3++;printf("%d %d\n",i,clo[i]);}
}
if(t3==1)
{
printf("There is 1 color left on the wall.\n");
}
else
printf("There are %d colors left on the wall.\n",t3);

}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: