hdu 5925 Coconuts 离散化+dfs
Coconuts
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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).
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.
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.
3 3
2
1 2
2 1
3 3
1
2 2
2
1 6
Case #2:
1
8
#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的更多相关文章
- HDU 5925 Coconuts 离散化
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5925 Coconuts Time Limit: 9000/4500 MS (Java/Others) ...
- HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
Coconuts Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- HDU 5925 Coconuts
2016 CCPC 东北四省赛 D. 一道好题. 现场写崩了. 赛后LSh跟我讲了一种离散化的做法, 没听懂. 题意 一个\(R \cdot C\ (R, C\le 10^9)\) 的矩形点阵上有 $ ...
- Coconuts HDU - 5925 (二维离散化求连通块的个数以及大小)
题目链接: D - Coconuts HDU - 5925 题目大意:首先是T组测试样例,然后给你n*m的矩阵,原先矩阵里面都是白色的点,然后再输入k个黑色的点.这k个黑色的点可能会使得原先白色的点 ...
- hdu4605 树状数组+离散化+dfs
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDOJ(HDU).2660 Accepted Necklace (DFS)
HDOJ(HDU).2660 Accepted Necklace (DFS) 点我挑战题目 题意分析 给出一些石头,这些石头都有自身的价值和重量.现在要求从这些石头中选K个石头,求出重量不超过W的这些 ...
- HDOJ(HDU).1045 Fire Net (DFS)
HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...
- HDOJ(HDU).1241 Oil Deposits(DFS)
HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- HDOJ(HDU).1035 Robot Motion (DFS)
HDOJ(HDU).1035 Robot Motion [从零开始DFS(4)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DF ...
随机推荐
- Delphi中使用@取函数地址的问题(转)
Delphi中使用@取函数地址的问题 例如以下代码:unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes ...
- JAVA学习方法
我觉得要想学好的话,首先,你得会用,能玩的起来,然后才会慢慢激发你的兴趣,它为何这么用?就这样一步一步的去探索,不断地积累,总结.编程讲究的是思维,死记硬背是没有意义的,想学好编程就一个字:敲,俩字 ...
- C#操作CSV存取类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- Linux驱动学习笔记(6)信号量(semaphore)与互斥量(mutex)【转】
转自:http://blog.chinaunix.net/uid-24943863-id-3193530.html 并发导致竟态,从而导致对共享数据的非控制访问,产生非预期结果,我们要避免竟态的发生. ...
- HDU 5961:传递(暴搜)
http://acm.hdu.edu.cn/showproblem.php?pid=5961 题意:中文题意.给出两个图,判断这个两个图是否都是传递的.注意一下传递的定义要看清,一开始没看清连样例都看 ...
- 每日一九度之 题目1023:EXCEL排序
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:18804 解决:4240 题目描述: Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似功能. 对每个测试用例 ...
- 最小生成树--Prim算法,基于优先队列的Prim算法,Kruskal算法,Boruvka算法,“等价类”UnionFind
最小支撑树树--Prim算法,基于优先队列的Prim算法,Kruskal算法,Boruvka算法,“等价类”UnionFind 最小支撑树树 前几节中介绍的算法都是针对无权图的,本节将介绍带权图的最小 ...
- Linux内核2.4.x的网络接口源码的结构[转]
http://blog.csdn.net/wswifth/article/details/5102242 一.前言 Linux的源码里,网络接口的实现部份是非常值得一读的,通过读源码,不仅对网络协议会 ...
- CentOS 安装SolrCloud
1.什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud.当一个系统的索引数据量少的时候是不 ...
- Linux 多核下绑定硬件中断到不同 CPU(IRQ Affinity) 转
硬件中断发生频繁,是件很消耗 CPU 资源的事情,在多核 CPU 条件下如果有办法把大量硬件中断分配给不同的 CPU (core) 处理显然能很好的平衡性能.现在的服务器上动不动就是多 CPU 多核. ...