HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
Coconuts
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 524 Accepted Submission(s): 151Problem 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 Input23 3
2
1 2
2 13 3
1
2 2Sample OutputCase #1:
2
1 6
Case #2:
1
8SourceRecommend
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5925
题目大意:
R行C列的网格(R,C<=109),有N(N<=200)个障碍,求联通块数量和各个大小(从小到大)。
题目思路:
【离散化+BFS】
首先障碍数量很小,所以可以离散化,把一大块没有障碍的网格缩成一个,权值A[i][j]为原先的小网格数。
缩网格只要以每一个障碍为中心画水平垂直线即可。细节注意一下
缩完之后有不超过400*400的矩形,暴力枚举一遍联通块,BFS,每个矩形走一次。即可得到各个联通块大小。排序输出即可
注意0的时候输出两行0.
(被这题卡了一晚上好气啊,写了随机数据把百度首页的代码全HACK掉了,数据打了崩溃溢出或者WA的。这也能AC吗。最后发现自己多打了一个+1。+1s)
//
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-10)
#define J 10000
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 1004
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans;
int nc,nn,mm;
LL s[N],r[N],rr[N],a[N][N];
int dx[]={-,,,};
int dy[]={,,-,};
bool u[N][N];
struct xxx
{
int x,y,x1,y1;
}c[N];
bool cmp1(xxx aa,xxx bb)
{
if(aa.x!=bb.x)return aa.x<bb.x;
return aa.y<bb.y;
}
bool cmp2(xxx aa,xxx bb)
{
if(aa.y!=bb.y)return aa.y<bb.y;
return aa.x<bb.x;
}
void spfa(int sx,int sy)
{
queue<int>q1,q2;
int i,j,x,y,xx,yy;
LL sum;
q1.push(sx),q2.push(sy);
u[sx][sy]=;sum=a[sx][sy];
while(!q1.empty())
{
x=q1.front(),q1.pop();
y=q2.front(),q2.pop();
for(j=;j<;j++)
{
xx=x+dx[j],yy=y+dy[j];
if(xx< || xx>nn || yy< || yy>mm || u[xx][yy])continue;
u[xx][yy]=;
q1.push(xx);q2.push(yy);
sum+=a[xx][yy];
}
}
s[++lll]=sum;
}
void print()
{
int i;
if(lll==){puts("0\n0");return;}
sort(s+,s++lll);
printf("%d\n",lll);
for(i=;i<=lll;i++)
printf("%lld%c",s[i],i==lll?'\n':' ');
}
int main()
{
#ifndef ONLINE_JUDGEW
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,z;
// init();
// for(scanf("%d",&cass);cass;cass--)
for(scanf("%d",&cas),cass=;cass<=cas;cass++)
// while(~scanf("%s",s))
// while(~scanf("%d%d",&n,&m))
{
lll=;mem(u,);
printf("Case #%d:\n",cass);
scanf("%d%d%d",&n,&m,&nc);
for(i=;i<=nc;i++)
{
scanf("%d%d",&x,&y);
if(x>n || y>m){i--,nc--;continue;}
c[i].x=x,c[i].y=y;
}
for(i=;i<N+N;i++)rr[i]=r[i]=;
nn=n,mm=m;
sort(c+,c++nc,cmp1);
for(i=;i<=nc;i++)
{
if(c[i].x-c[i-].x>)
{
nn-=c[i].x-c[i-].x-;
c[i].x1=c[i-].x1+;
r[c[i-].x1+]*=c[i].x-c[i-].x-;
}
else c[i].x1=c[i-].x1+c[i].x-c[i-].x;
}
if(nn!=c[nc].x1)r[c[nc].x1+]*=nn-c[nc].x1,nn=c[nc].x1+; sort(c+,c++nc,cmp2);
for(i=;i<=nc;i++)
{
if(c[i].y-c[i-].y>)
{
mm-=c[i].y-c[i-].y-;
c[i].y1=c[i-].y1+;
rr[c[i-].y1+]*=c[i].y-c[i-].y-;
}
else c[i].y1=c[i-].y1+c[i].y-c[i-].y;
}
if(mm!=c[nc].y1)rr[c[nc].y1+]*=mm-c[nc].y1,mm=c[nc].y1+; for(i=;i<=nc;i++)u[c[i].x1][c[i].y1]=;
for(i=;i<=nn;i++)
for(j=;j<=mm;j++)
a[i][j]=r[i]*rr[j];
for(i=;i<=nn;i++)
{
for(j=;j<=mm;j++)
{
if(u[i][j])continue;
spfa(i,j);
}
}
print();
}
return ;
}
/*
// //
*/
HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)的更多相关文章
- HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- HDU 5926 Mr. Frog's Game 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Mr. Frog's Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 5922 Minimum’s Revenge 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Minimum's Revenge Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)
Auxiliary Set Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- HDU 5923 Prediction(2016 CCPC东北地区大学生程序设计竞赛 Problem B,并查集)
题目链接 2016 CCPC东北地区大学生程序设计竞赛 B题 题意 给定一个无向图和一棵树,树上的每个结点对应无向图中的一条边,现在给出$q$个询问, 每次选定树中的一个点集,然后真正被选上的是这 ...
- 2016CCPC东北地区大学生程序设计竞赛1008/HDU 5929 模拟
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 2016CCPC东北地区大学生程序设计竞赛 (2018年8月22日组队训练赛)
题目链接:http://acm.hdu.edu.cn/search.php?field=problem&key=2016CCPC%B6%AB%B1%B1%B5%D8%C7%F8%B4%F3%D ...
- 2016CCPC东北地区大学生程序设计竞赛 1008 HDU5929
链接http://acm.hdu.edu.cn/showproblem.php?pid=5929 题意:给你一种数据结构以及操作,和一种位运算,最后询问:从'栈'顶到低的运算顺序结果是多少 解法:根据 ...
随机推荐
- html学习的一些问题
1,什么是 W3C标准?w3c 标准不是一个标准,而是一系列标准,包括:结构标准,表现标准,动作标准. 2,内链元素和块状元素的区别内链元素允许与其他内链元素位于同一行,没有宽和高,如果想设置宽和搞, ...
- 组策略彻底解决windows 2003 终端数
win2003的话可以从组策略修改: 组策略级别要高于终端服务配置,当启用组策略后终端服务配置中的相应选项会变成灰色不可修改 运行-gpedit.msc-计算机配置-管理模板-Windows ...
- vc调用BCB的dll 参数传递 报错
可能原因: 调用方式约定不一致. 函数调用约定如下: 1. __cdecl:C 和 C++ 程序的缺省调用规范. 2. __stdcall:标准调用约定(即WINAPI调用约定),也就是pascal调 ...
- Using native GDI for text rendering in C#
Using native GDI for text rendering in C# Aug12by Arthur To complete my previous post on text render ...
- jstl的formatNumber标签的四舍五入问题
jstl的formatNumber标签的四舍五入问题 近日使用JSTL的formatNumber 标签进行四舍五入时,发现它竟然使用的是"4舍6入5奇偶"的算法. 要实现" ...
- 利用查询提示优化SQL
数据库环境:SQL SERVER 2005 我们来看一下SQL语句及对应的数据量 SELECT COUNT(*) FROM cinvout_02 a WHERE ( a.dept_id IN ( SE ...
- delphi参数传递
delphi参数传递 参数传递 声明/实现一个过程使用的参数称为形式参数(简称形参),调用过程时传入的参数称为实际参数(简称实参). { Info是形参} procedure ShowInfo( ...
- CSS 组合选择符
CSS 组合选择符 组合选择符说明了两个选择器直接的关系. CSS组合选择符包括各种简单选择符的组合方式. 在 CSS3 中包含了四种组合方式: 后代选取器(以空格分隔) 子元素选择器(以大于号分隔) ...
- windows注册表的基本使用——示例
上网找好多资料发现一个问题就是太老.例如只有RegCreateKey而没有RegCreateKeyEx用法详解,自己摸索了几个小时终于基本用到的几个函数都试用了一遍. 下面代码已经通过编译测试 // ...
- ava下static关键字用法详解
Java下static关键字用法详解 本文章介绍了java下static关键字的用法,大部分内容摘自原作者,在此学习并分享给大家. Static关键字可以修饰什么? 从以下测试可以看出, static ...