题目链接:

D - Coconuts

 HDU - 5925

题目大意:首先是T组测试样例,然后给你n*m的矩阵,原先矩阵里面都是白色的点,然后再输入k个黑色的点。这k个黑色的点可能会使得原先白色的点分成多个联通块,然后问你白色联通块的个数和每一个连通块里面白色的点的个数。

具体思路:输入的点坐标比较大,但是个数比较小,所以离散化坐标就可以了。标记每一个整块里面的白色的点的个数就可以了。

AC代码:

 #include<bits/stdc++.h>
using namespace std;
# define ll long long
# define inf 0x3f3f3f3f
const int maxn = 2e5+;
const int maxm =+;
vector<ll>contx;
vector<ll>conty;// 注意开long long
int vis[maxm<<][maxm<<];
ll a[maxm<<],cnt;
ll Map[maxm<<][maxm<<];
int f[][]= {{,-,,},{,,,-}};
struct node
{
int x,y;
node() {}
node(int xx,int yy)
{
x=xx;
y=yy;
}
} q[maxn];
bool judge(int x,int y)
{
if(x>=&&y>=&&x<=contx.size()&&y<=conty.size())
return true;
return false;
}
void dfs(int x,int y)
{
cnt+=Map[x][y];
vis[x][y]=;
for(int i=; i<; i++)
{
int tx=x+f[][i];
int ty=y+f[][i];
if(judge(tx,ty)&&vis[tx][ty]==)
dfs(tx,ty);
}
}
int main()
{
int T,Case=;
scanf("%d",&T);
while(T--)
{
int n,m,tot;
scanf("%d %d",&n,&m);
memset(vis,,sizeof(vis));
memset(Map,,sizeof(Map));
contx.clear();
conty.clear();
scanf("%d",&tot);
contx.push_back();
conty.push_back();
contx.push_back(n);
conty.push_back(m); /// 先把四个角输进去
for(int i=; i<=tot; i++)
{
scanf("%d %d",&q[i].x,&q[i].y);
contx.push_back(q[i].x);
conty.push_back(q[i].y);
if(q[i].x->=)
contx.push_back(q[i].x-);
if(q[i].x+<=n)
contx.push_back(q[i].x+);
if(q[i].y->=)
conty.push_back(q[i].y-);
if(q[i].y+<=m)
conty.push_back(q[i].y+);
}
sort(contx.begin(),contx.end());
sort(conty.begin(),conty.end());
contx.erase(unique(contx.begin(),contx.end()),contx.end());
conty.erase(unique(conty.begin(),conty.end()),conty.end());
for(int i=; i<=tot; i++)
{
int t1=lower_bound(contx.begin(),contx.end(),q[i].x)-contx.begin()+;
int t2=lower_bound(conty.begin(),conty.end(),q[i].y)-conty.begin()+;
vis[t1][t2]=;
}
for(int i=; i<contx.size(); i++)
{
for(int j=; j<conty.size(); j++)
{
if(i>&&j>)
Map[i+][j+]=(contx[i]-contx[i-])*(conty[j]-conty[j-]);
else if(i>)
Map[i+][j+]=(contx[i]-contx[i-])*conty[j];
else if(j>)
Map[i+][j+]=contx[i]*(conty[j]-conty[j-]);
else
Map[i+][j+]=contx[i]*conty[j];
}
}
int num=;
for(int i=; i<=contx.size(); i++)
{
for(int j=; j<=conty.size(); j++)
{
if(vis[i][j])continue;
cnt=;
dfs(i,j);
a[++num]=cnt;
}
}
sort(a+,a+num+);
printf("Case #%d:\n",++Case);
printf("%d\n",num);
for(int i=; i<=num; i++)
{
if(i==)
printf("%lld",a[i]);
else
printf(" %lld",a[i]);
}
printf("\n");
}
return ;
}

 

Coconuts HDU - 5925 (二维离散化求连通块的个数以及大小)的更多相关文章

  1. Coconuts HDU - 5925 二维离散化 自闭了

    TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams about eating. One day, ...

  2. hdu 3072 Intelligence System(Tarjan 求连通块间最小值)

    Intelligence System Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  3. DFS入门之二---DFS求连通块

    用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...

  4. [C++]油田(Oil Deposits)-用DFS求连通块

    [本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...

  5. HDU1241 Oil Deposits —— DFS求连通块

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...

  6. UVA 572 Oil Deposits油田(DFS求连通块)

    UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format: ...

  7. UVA 572 dfs求连通块

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...

  8. C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块

    C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  9. UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

    UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...

随机推荐

  1. 牛客网暑期ACM多校训练营 第九场

    HPrefix Sum study from : https://blog.csdn.net/mitsuha_/article/details/81774727 k较小.分离x和k. 另外的可能:求a ...

  2. codesmith生成的结果页不显示,问题在于第一行的文件头

    在于这里: TargetLanguage="C#",这个能增加cs的格式

  3. 关于shared_ptr与weak_ptr的使用(good)

    shared_ptr是带引用计数的智能指针,可以说大部分的情形选择用shared_ptr不会出问题.那么weak_ptr是什么,应该怎么用呢? weak_ptr也是智能指针,但是比较弱,感觉没什么用. ...

  4. (叉乘求面积) nyoj1011-So Easy[II]

    1011-So Easy[II] 内存限制:64MB 时间限制:1000ms 特判: No通过数:2 提交数:4 难度:2 题目描述: 这是一道基础的计算几何问题(其实这不提示大家也都看的出).问题描 ...

  5. python3: 爬虫---- urllib, beautifulsoup

    最近晚上学习爬虫,首先从基本的开始: python3 将urllib,urllib2集成到urllib中了, urllib可以对指定的网页进行请求下载,  beautifulsoup 可以从杂乱的ht ...

  6. chrome截图全网页

    1.F12 2.ctrl+shift+p 3.输入:capture 4.选择Capture full size screenshot

  7. Golang入门教程(十二)安装注意事项

    1.$GOPATH (1)go 命令依赖一个重要的环境变量:$GOPATH .注:这个不是Go安装目录 (2) (3) (4)git 安装 (5)包管理对应关系 (6)安装完之后bee 工具后,bee ...

  8. 解决svn检出后不显示图标的问题

    解决svn检出后不显示图标的问题: 此文经过个人验证,可以解决TortoiseSVN图标显示异常问题: 问题出现原因:Windows Explorer Shell 支持 Overlay Icon 最多 ...

  9. 细说java系列之泛型

    什么是范型 简言之,范型是Java支持在编译期进行类型检查的机制. 这里面包含2层含义:其一,可以使用范型进行类型检查:其二,在编译期进行类型检查. 那么,什么叫做在编译期进行类型检查?可以在运行时进 ...

  10. Install Ubuntu Server

    进入引导程序以后, 选择Install Ubuntu Server, 安装主菜单如下: 依次配置: 接着 https://www.youtube.com/watch?v=gqLaT01yei0