Coconuts

Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Problem Description
TanBig,
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).

 
Input
The 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.

 
Output
For
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
2

3 3
2
1 2
2 1

3 3
1
2 2

 
Sample Output
Case #1:
2
1 6
Case #2:
1
8
 
Source
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e3+,M=4e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
int n,m,q;
struct point
{
int x,y;
}a[N];
ll l[N];
int getpos(int x,int flag)
{
return lower_bound(l,l+flag,x)-l;
}
int mp[N][N];
int vis[N][N];
int xx[]={,,,-};
int yy[]={-,,,};
int flag;
int nn,mm;
int check(int x,int y)
{
if(x<||x>nn||y<||y>mm)
return ;
return ;
}
void dfs(int n,int m,int deep)
{
vis[n][m]=deep;
for(int i=;i<;i++)
{
int xxx=n+xx[i];
int yyy=m+yy[i];
if(check(xxx,yyy)&&!mp[xxx][yyy]&&!vis[xxx][yyy])
{
dfs(xxx,yyy,deep);
}
}
}
ll ans[];
ll getnum(int x)
{
if(x==)
return l[x];
return l[x]-l[x-];
}
int main()
{
int T,cas=;
scanf("%d",&T);
while(T--)
{
flag=;
memset(mp,,sizeof(mp));
memset(vis,,sizeof(vis));
memset(ans,,sizeof(ans));
int kuai=;
scanf("%d%d",&n,&m);
scanf("%d",&q);
for(int i=;i<=q;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
l[flag++]=a[i].x;
l[flag++]=a[i].y;
if(a[i].x+<=n)
l[flag++]=a[i].x+;
if(a[i].y+<=m)
l[flag++]=a[i].y+;
if(a[i].x-)
l[flag++]=a[i].x-;
if(a[i].y-)
l[flag++]=a[i].y-;
}
l[flag++]=;
if(n>=||m>=)
l[flag++]=;
l[flag++]=n;
l[flag++]=m;
sort(l,l+flag);
flag=unique(l,l+flag)-l;
for(int i=;i<=q;i++)
{
mp[getpos(a[i].x,flag)][getpos(a[i].y,flag)]=;
}
nn=getpos(n,flag);
mm=getpos(m,flag);
for(int i=;i<=nn;i++)
{
for(int t=;t<=mm;t++)
{
if(!mp[i][t]&&!vis[i][t])
{
dfs(i,t,kuai++);
}
}
}
for(int i=;i<=nn;i++)
{
for(int t=;t<=mm;t++)
{
if(vis[i][t])
{
ans[vis[i][t]]+=getnum(i)*getnum(t);
}
}
}
printf("Case #%d:\n",cas++);
printf("%d\n",kuai-);
if(kuai-)
{
sort(ans+,ans+kuai);
printf("%lld",ans[]);
for(int i=;i<kuai;i++)
printf(" %lld",ans[i]);
printf("\n");
}
}
return ;
}

hdu 5925 Coconuts 离散化+dfs的更多相关文章

  1. HDU 5925 Coconuts 离散化

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5925 Coconuts Time Limit: 9000/4500 MS (Java/Others) ...

  2. HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)

    Coconuts Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  3. HDU 5925 Coconuts

    2016 CCPC 东北四省赛 D. 一道好题. 现场写崩了. 赛后LSh跟我讲了一种离散化的做法, 没听懂. 题意 一个\(R \cdot C\ (R, C\le 10^9)\) 的矩形点阵上有 $ ...

  4. Coconuts HDU - 5925 (二维离散化求连通块的个数以及大小)

    题目链接: D - Coconuts  HDU - 5925 题目大意:首先是T组测试样例,然后给你n*m的矩阵,原先矩阵里面都是白色的点,然后再输入k个黑色的点.这k个黑色的点可能会使得原先白色的点 ...

  5. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  6. HDOJ(HDU).2660 Accepted Necklace (DFS)

    HDOJ(HDU).2660 Accepted Necklace (DFS) 点我挑战题目 题意分析 给出一些石头,这些石头都有自身的价值和重量.现在要求从这些石头中选K个石头,求出重量不超过W的这些 ...

  7. HDOJ(HDU).1045 Fire Net (DFS)

    HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...

  8. HDOJ(HDU).1241 Oil Deposits(DFS)

    HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  9. HDOJ(HDU).1035 Robot Motion (DFS)

    HDOJ(HDU).1035 Robot Motion [从零开始DFS(4)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DF ...

随机推荐

  1. HID USB设备开发技术【转】

    本文转载自: 1.高速USB和USB2.0有区别吗?     高速USB和USB2.0是有区别的,区别在于USB2.0是一种规范,而"高速USB"仅指在USB2.0规范中数据传输率 ...

  2. 【python cookbook】【字符串与文本】16.以固定的列数重新格式化文本

    问题:重新格式化一些很长的字符串,以指定的列数来显示 解决方案:textwrap模块的fill()方法来实现 # A long string s = "Look into my eyes, ...

  3. 精通 JS正则表达式

    一.正则表达式可以: 测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式或一个信用卡号码模式.这称为数据有效性验证 替换文本.可以在文档中使用一个正则表达式 ...

  4. MyBatis关联查询分页

    背景:单表好说,假如是MySQL的话,直接limit就行了. 对于多对多或者一对多的情况,假如分页的对象不是所有结果集,而是对一边分页,那么可以采用子查询分页,再与另外一张表关联查询,比如: sele ...

  5. JDK结构介绍

    dt.jar和tools.jar位于:{Java_Home}/lib/下, 而rt.jar位于:{Java_Home}/jre/lib/下, 其中: (1) rt.jar是JAVA基础类库,也就是你在 ...

  6. MongoDB Shell

    MongoDB Shell   1.连接指定主机及数据库 mongo 127.0.0.1:30000/myDB 链接到127.0.0.1的30000端口的myDB 2.启动后连接指定数据库 *.&qu ...

  7. Linux C socket 编程之TCP

    推荐:http://www.cnblogs.com/tianshuai11/archive/2011/11/30/2477224.html

  8. poj1988 Cube Stacking

    并查集的高效之处在于路径压缩和延迟更新. 在本题中需要额外维护子树的规模以及当前子树节点到跟的距离两个数组. 由于一个新的数必然是两棵树拼接而成,对于子树规模的更新直接相加即可, 对于节点到跟的距离: ...

  9. 异步I/O

    http://blog.csdn.net/gotosola/article/details/7412409 Linux2.6异步I/O AIO的基本思想:  允许进程发起很多I/O操作,而不用阻塞或等 ...

  10. Uva 1220,Hali-Bula 的晚会

    题目链接:https://uva.onlinejudge.org/external/12/1220.pdf 题意: 公司n个人,形成一个数状结构,选出最大独立集,并且看是否是唯一解. 分析: d(i) ...