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启动的服务
1. 查看Linux启动的服务chkconfig --list 查询出所有当前运行的服务chkconfig --list atd 查询atd服务的当前状态 2.停止所有服务并且在下次系统启动时不再启 ...
- windows cmd color setup
设置颜色的话,一般可定会有foreground和background color设置:(其实color /?直接看一下就好了) Color Background Foreground Black 0 ...
- XML 解析 -- IE ,Chrome
通用的method <script type="text/javascript"> //for chrome, attach method to XMLDocument ...
- phpize 动态添加 PHP 扩展的错误及解决方案
使用phpize 动态添加 PHP 扩展是开发中经常需要做的事情,但是在 macOS 中,首次使用该功能必然会碰到一些错误,本文列出了这些错误的解决方法. 问题一: 执行 phpize 报错如下: $ ...
- StringBuffer类的方法
public class Page116 { /** * StringBuffer类的练习 * @param args * @throws IOException */ public static v ...
- prop
用法:prop(属性|key,value|fn) 用例:点击全选/取消全选 // 全选 和全不选 $("#check_all").click(function () { if ($ ...
- RTC系统
http://blog.csdn.net/fanqipin/article/details/8089995 一. RTC及驱动简介 RTC即real time clock实时时钟,主要用于为操作系统提 ...
- SIM卡应用-OPN,PLMN,SPN
SIM卡应用 移动运营商已经将SIM卡用於很多不同的应用,下面列出了其中最主要的应 用∶ ·漫游应用∶确保手机可以在漫游之後选择缺省的运营商网络.一个SIM应用是可以在手机漫游到某个合作夥伴运营商网络 ...
- c#线程创建
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- SVG 是什么?
SVG 意为可缩放矢量图形(Scalable Vectors Graphics). SVG 图形在放大或者改变尺寸的情况下,其图形的质量不会有所损失. SVG 使用 XML 格式定义图形. ...