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 ...
随机推荐
- linux设备驱动归纳总结(四):2.进程调度的相关概念【转】
本文转载自:http://blog.chinaunix.net/uid-25014876-id-65555.html linux设备驱动归纳总结(四):2.进程调度的相关概念 xxxxxxxxxxxx ...
- 160905、c3p0详细配置
官方文档 : http://www.mchange.com/projects/c3p0/index.html <c3p0-config> <default-config> &l ...
- IE6不支持CSS的属性选择器
input[type="text"] { width: 50px; } 测试IE6不生效,而IE7以上浏览器则没问题
- 【Pro ASP.NET MVC 3 Framework】.学习笔记.3.MVC的主要工具-单元测试
IProductRepository接口定义了一个仓库,我们通过它获得.更新Product对象.IPriceReducer接口指定了一个功能,它将要对所有的Products实施,通过一个参数,降低他们 ...
- Oracle PL/SQL之LOOP循环控制语句
在PL/SQL中可以使用LOOP语句对数据进行循环处理,利用该语句可以循环执行指定的语句序列.常用的LOOP循环语句包含3种形式:基本的LOOP.WHILE...LOOP和FOR...LOOP. LO ...
- ecshop订单打印页显示商品缩略图和序号
ecshop订单打印页显示商品缩略图和序号 订单打印页显示商品缩略图,在论坛没找到适合2.7.2相关的文章,特意贴上来给大家研究一下.1.找到 $sql = "SELECT o.*, IF( ...
- SQL Server 2005 日志文件过大处理
由于安装的时候没有计划好空间,默认装在系统盘,而且又没有做自动备份.截断事务日志等,很快LDF文件就达到十几G,或者几十G ,此时就不得不处理了. 备份和计划就不说了,现在就说下怎么把它先删除吧: 1 ...
- Effective C++第三遍
试图调用private的copy或赋值函数是编译期错误,而调用没有具体定义的函数则是连接期错误. 以对象管理资源:智能指针RAII(资源获取立即初始化)后都是对象,但有时候,比如(API的)函数参数要 ...
- 我的CSS样式记事本(1)
文本 行高: line-height 对齐方式: text-align 字符间距: letter-spacing 文本修饰: text-decoration字体 设置字体所有: font 字体类型: ...
- 算法心得1:由$nlogn$复杂度的LIS算法引起的思考
LIS(Longest Increasing Subsequence)是一类典型的动态规划类问题,简化描述如下: 给定$N(n) = \{1,2...,n\}$的一个排列$P(n)$,求$P(n)$中 ...