USACO SECTION 3.1 Shaping Regions

Shaping Regions

N opaque rectangles (1 <= N <= 1000) of various colors are placed on a white sheet of paper whose size is A wide by B long. The rectangles are put with their sides parallel to the sheet's borders. All rectangles fall within the borders of the sheet so that different figures of different colors will be seen.

The coordinate system has its origin (0,0) at the sheet's lower left corner with axes parallel to the sheet's borders.

PROGRAM NAME: rect1

INPUT FORMAT

The order of the input lines dictates the order of laying down the rectangles. The first input line is a rectangle "on the bottom".

Line 1:A, B, and N, space separated (1 <= A,B <= 10,000)
Lines 2-N+1:Five integers: llx, lly, urx, ury, color: the lower left coordinates and upper right coordinates of the rectangle whose color is `color' (1 <= color <= 2500) to be placed on the white sheet. The color 1 is the same color of white as the sheet upon which the rectangles are placed.

SAMPLE INPUT (file rect1.in)

20 20 3
2 2 18 18 2
0 8 19 19 3
8 0 10 19 4

INPUT EXPLANATION

Note that the rectangle delineated by 0,0 and 2,2 is two units wide and two high. Here's a schematic diagram of the input:
11111111111111111111
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
11222222442222222211
11222222442222222211
11222222442222222211
11222222442222222211
11222222442222222211
11222222442222222211
11111111441111111111
11111111441111111111
The '4's at 8,0 to 10,19 are only two wide, not three (i.e., the grid contains a 4 and 8,0 and a 4 and 8,1 but NOT a 4 and 8,2 since this diagram can't capture what would be shown on graph paper).

OUTPUT FORMAT

The output file should contain a list of all the colors that can be seen along with the total area of each color that can be seen (even if the regions of color are disjoint), ordered by increasing color. Do not display colors with no area.

SAMPLE OUTPUT (file rect1.out)

1 91
2 84
3 187
4 38

 

Executing...
   Test 1: TEST OK [0.000 secs, 16248 KB]
   Test 2: TEST OK [0.000 secs, 16248 KB]
   Test 3: TEST OK [0.000 secs, 16248 KB]
   Test 4: TEST OK [0.000 secs, 16248 KB]
   Test 5: TEST OK [0.000 secs, 16248 KB]
   Test 6: TEST OK [0.000 secs, 16248 KB]
   Test 7: TEST OK [0.000 secs, 16248 KB]
   Test 8: TEST OK [0.000 secs, 16248 KB]
   Test 9: TEST OK [0.000 secs, 16248 KB]
   Test 10: TEST OK [0.000 secs, 16248 KB]
   Test 11: TEST OK [0.454 secs, 16248 KB]

/*
ID: conicoc1
LANG: C
TASK: rect1
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct Rectangle
{
	int llx;
	int lly;
	int urx;
	int ury;
	int color;
};

int N,RecN;
int Paper[1900][1900],Wide,Long;
int XSort[2002],YSort[2002];
int Color[2501];

int comp(const void *a,const void *b)
{
	return *(int *)a-*(int *)b;
}

void Cover(struct Rectangle A)
{
	int X1,X2,Y1,Y2;
	int sum;
	int i,j;
	for(X1=1,sum=0;A.llx!=sum;X1++)
		sum+=Paper[X1][0];
	for(X2=X1;A.urx!=sum;X2++){
		sum+=Paper[X2][0];
	}
		
	for(Y1=1,sum=0;A.lly!=sum;Y1++)
		sum+=Paper[0][Y1];
	for(Y2=Y1;A.ury!=sum;Y2++){
		sum+=Paper[0][Y2];
	}

	for(i=X1;i<X2;i++){
		for(j=Y1;j<Y2;j++){
			Paper[i][j]=A.color;
		}
	}	
}

int main()
{
	FILE *fin,*fout;
	fin=fopen("rect1.in","r");
	fout=fopen("rect1.out","w");
	struct Rectangle Rec[1001];
	int i,j,k,sum;	
	fscanf(fin,"%d %d %d",&Rec[0].urx,&Rec[0].ury,&N);
	for(i=1,RecN=1;i<N+1;i++){
		fscanf(fin,"%d %d %d %d %d",&Rec[i].llx,&Rec[i].lly,&Rec[i].urx,&Rec[i].ury,&Rec[i].color);
		RecN++;
	}
	Rec[0].llx=0,Rec[0].lly=0,Rec[0].color=1;	
	memset(Color,0,sizeof(Color));
	//初始化 
	
	for(i=0,j=0,k=0;i<N+1;i++){
		XSort[j++]=Rec[i].llx;
		XSort[j++]=Rec[i].urx;
		YSort[k++]=Rec[i].lly;
		YSort[k++]=Rec[i].ury;
	}
	
	qsort(XSort,j,sizeof(int),comp);
	qsort(YSort,k,sizeof(int),comp);

	for(i=0,Wide=1;i<j-1;i++){               //离散化 
		if(XSort[i]==XSort[i+1])
			continue;
		Paper[Wide++][0]=XSort[i+1]-XSort[i];
	}
	for(i=0,Long=1;i<k-1;i++){
		if(YSort[i]==YSort[i+1])
			continue;
		Paper[0][Long++]=YSort[i+1]-YSort[i];
	}

	for(i=0;i<N+1;i++){            //依次覆盖 
		Cover(Rec[i]);
	}
	
	
	for(i=1;i<Wide;i++){        //统计数量 
		for(j=1;j<Long;j++){
			Color[Paper[i][j]]+=Paper[i][0]*Paper[0][j];
		}
	}
	for(i=1;i<=2500;i++){
		if(Color[i]!=0)
			fprintf(fout,"%d %d\n",i,Color[i]);
	}	
	return 0;
}


又是参考了矩阵大神博客里的文章

用离散化写的

这里USACO的数据弱了

开2000*2000的数组显然空间爆了

如果每个rectangle的x,y坐标都没有重复的话,空间就超了

 

另外有矩阵切割的方法,去研究研究

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值