国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

HDU 5929 Coconuts(離散化+dfs)

2019-11-08 02:43:43
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Coconuts

PRoblem DescriptionTanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams about eating. One day, TanBig dreams of a field of coconuts, and the field looks like a large chessboard which has R rows and C columns. In every cell of the field, there is one coconut. Unfortunately, some of the coconuts have gone bad. For sake of his health, TanBig will eat the coconuts following the rule that he can only eat good coconuts and can only eat a connected component of good coconuts one time(you can consider the bad coconuts as barriers, and the good coconuts are 4-connected, which means one coconut in cell (x, y) is connected to (x - 1, y), (x + 1, y), (x, y + 1), (x, y - 1).Now TanBig wants to know how many times he needs to eat all the good coconuts in the field, and how many coconuts he would eat each time(the area of each 4-connected component). InputThe first line contains apositiveinteger T(T≤10) which denotes the test cases. T test cases begin from the second line. In every test case, the first line contains two integers R and C, 0<R,C≤109 the second line contains an integer n, the number of bad coconuts, 0≤n≤200 from the third line, there comes n lines, each line contains two integers, xi and yi, which means in cell(xi,yi), there is a bad coconut.It is guaranteed that in the input data, the first row and the last row will not have bad coconuts at the same time, the first column and the last column will not have bad coconuts at the same time. OutputFor each test case, output "Case #x:" in the first line, where x denotes the number of test case, one integer k in the second line, denoting the number of times TanBig needs, in the third line, k integers denoting the number of coconuts he would eat each time, you should output them in increasing order. Sample Input
23 321 22 13 312 2 Sample Output
Case #1:21 6Case #2:18

一個(gè)求有多少個(gè)聯(lián)通塊的題目,數(shù)據(jù)量比較大,需要對(duì)點(diǎn)的坐標(biāo)分別離散化。

#include <iostream>#include <algorithm>#include <vector>#include <cstring>#include <string>#include <cstdio>using namespace std;const int Size = 1024;int T;//測(cè)試數(shù)據(jù)int Case = 1;int R,C;int Q;vector<long long>Area;//存儲(chǔ)每一塊聯(lián)通塊的面積int x[Size];int y[Size];int temp[Size];int lenx[Size];//x方向每一段的長(zhǎng)度;int leny[Size];//y方向每一段的長(zhǎng)度;int Pos[Size];int cntx,cnty;bool book[Size][Size];int dx[] = {1, -1, 0, 0};int dy[] = {0, 0, -1, 1};long long dfs(int x, int y){	long long sum = (long long)lenx[x]*leny[y];	book[x][y] = true;	for(int i=0; i<4; i++)	{		int _x = x+dx[i];		int _y = y+dy[i];		if(!book[_x][_y] && _x>=1 && _x<=cntx && _y>=1 && _y<=cnty)			sum += dfs(_x,_y);	}	return sum;}int main(){	cin>>T;	while(T--)	{		//Initial		Area.clear();		//Input		cin>>R>>C;		cin>>Q;		for(int i=0; i<Q; i++)			cin>>x[i]>>y[i];		//離散化x軸		int index = 0;		temp[index++] = 0;		temp[index++] = R;		for(int i=0; i<Q; i++)			temp[index++] = x[i];//將bad coconuts的x坐標(biāo)以及邊界存起來(lái)		sort(temp, temp+index);//對(duì)x坐標(biāo)排序		index = unique(temp, temp+index)-temp;//對(duì)x坐標(biāo)去重		cntx = 0;//x軸離散的段數(shù)		for(int i=1; i<index; i++)//求每一段的長(zhǎng)度		{			if(temp[i] > temp[i-1]+1)				lenx[++cntx] = temp[i]-(temp[i-1]+1);			lenx[++cntx] = 1;			Pos[i] = cntx;//bad coconuts所在的段數(shù)		}		for(int i=0; i<Q; i++)		{			int t = lower_bound(temp,temp+index,x[i])-temp;			x[i] = Pos[t];//離散后更新原坐標(biāo)		}		//離散化y軸		index = 0;		temp[index++] = 0;		temp[index++] = C;		for(int i=0; i<Q; i++)			temp[index++] = y[i];		sort(temp, temp+index);		index = unique(temp, temp+index)-temp;		cnty = 0;		for(int i=1; i<index; i++)		{			if(temp[i] > temp[i-1]+1)				leny[++cnty] = temp[i]-(temp[i-1]+1);			leny[++cnty] = 1;			Pos[i] = cnty;		}		for(int i=0; i<Q; i++)		{			int t = lower_bound(temp, temp+index, y[i])-temp;			y[i] = Pos[t];		}		//DFS		memset(book, false, sizeof(book));		for(int i=0; i<Q; i++)			book[x[i]][y[i]] = true;		for(int i=1; i<=cntx; i++)			for(int j=1; j<=cnty; j++)				if(!book[i][j])					Area.push_back(dfs(i, j));		//output		sort(Area.begin(), Area.end());		int len = (int)Area.size();		cout<<"Case #"<<(Case++)<<":/n"<<len<<"/n";		for(int i=0; i<len-1; i++)			cout<<Area[i]<<" ";		cout<<Area[len-1]<<endl;	}}


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 栖霞市| 闻喜县| 邳州市| 连平县| 花莲县| 长汀县| 边坝县| 灵丘县| 沧州市| 通渭县| 兖州市| 正蓝旗| 中超| 福鼎市| 天等县| 华阴市| 招远市| 封开县| 青州市| 武夷山市| 和田市| 鞍山市| 郴州市| 宁南县| 西乡县| 永靖县| 青铜峡市| 奉贤区| 集安市| 鹿邑县| 交口县| 改则县| 英山县| 武义县| 凤山市| 肇源县| 莆田市| 柏乡县| 霍邱县| 金寨县| 内丘县|