cdoj203-Islands 【并查集】
http://acm.uestc.edu.cn/#/problem/show/203
Islands
Time Limit: 30000/10000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
Deep in the Carribean, there is an island even stranger than the Monkey Island, dwelled by Horatio Torquemada Marley. Not only it has a rectangular shape, but is also divided into an n×m grid. Each grid field has a certain height. Unfortunately, the sea level started to raise and in year i, the level is i meters. Another strange feature of the island is that it is made of sponge, and the water can freely flow through it. Thus, a grid field whose height is at most the current sea level is considered flooded.Adjacent unflooded fields (i.e., sharing common edge) create unflooded areas. Sailors are interested in the number of unflooded areas in a given year.
An example of a 4×5 island is given below. Numbers denote the heights of respective fields in meters.Unflooded fields are darker; there are two unflooded areas in the first year and three areas in the second year.
Input
Multiple Test Cases
The input contains several test cases. The first line of the input contains a positive integer Z≤20,denoting the number of test cases. Then Z test cases follow, each conforming to the format described in section Single Instance Input. For each test case, your program has to write an output conforming to the format described in section Single Instance Output.
Single Instance Input
The first line contains two numbers n and m separated by a single space, the dimensions of the island, where 1≤n,m≤1000. Next n lines contain m integers from the range [1,109] separated by single spaces, denoting the heights of the respective fields. Next line contains an integer T (1≤T≤105). The last line contains T integers tj , separated by single spaces, such that 0≤t1≤t2≤⋯≤tT≤109
Output
Single Instance Output
Your program should output a single line consisting of T numbers rj , where rj is the number of unflooded areas in year tj . After every number ,you must output a single space!
Sample input and output
Sample Input | Sample Output |
---|---|
1 |
2 3 1 0 0 |
思路:
这种联通块的问题一看就知道是并查集的思想。
做法:从高水位到低水位依序进行操作,这样每次都有新的块浮出水面,可以在前面的基础上进行合并集合的操作。
给每个位置分配一个数字,方便合并集合。同时将这些数字也排一个序,降低枚举的复杂度。合并集合时向四周查询浮出水面但是没有合并到同一集合的点进行合并。
代码:
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib> using namespace std; #define PI acos(-1.0)
#define EPS 1e-20
#define lll __int64
#define ll long long
#define INF 0x7fffffff
#define INT 2147483646
const int N=; struct node{
int x,y,h;
}a[N*N]; int n,m,t;
int tt[N*],fa[N*N],cnt[N*];
int xy[][]={{,,,-},{,-,,}}; bool cmp(const node &r1,const node &r2);
inline bool Check(int x,int y);
inline int Find(int x); int main(){
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
int T;
scanf("%d",&T);
while(T--){
int tn=;
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
for(int j=;j<m;j++){
scanf("%d",&a[tn].h);
a[tn].x=i;
a[tn++].y=j;
}
}
sort(a,a+n*m,cmp);
scanf("%d",&t);
for(int i=;i<t;i++){
scanf("%d",&tt[i]);
}
memset(fa,-,sizeof(fa));
memset(cnt,,sizeof(cnt));
for(int i=t-,j=;i>=;i--){
cnt[i]=cnt[i+];
while(){
if(j>=tn) break;
int tm=a[j].x*m+a[j].y;
if(a[j].h<=tt[i]||fa[tm]!=-) break;
cnt[i]++,fa[tm]=tm;
for(int k=;k<;k++){
int tx=a[j].x+xy[][k];
int ty=a[j].y+xy[][k];
if(Check(tx,ty)){
int it=tx*m+ty;
if(fa[it]!=-&&Find(it)!=fa[tm]) fa[fa[it]]=fa[tm],cnt[i]--;
}
}
j++;
}
}
for(int i=;i<t;i++)
printf("%d ",cnt[i]);
puts("");
}
return ;
}
bool cmp(const node &r1,const node &r2){
return r1.h>r2.h;
}
inline bool Check(int x,int y){
return x>=&&x<n&&y>=&&y<m;
}
inline int Find(int x){
return fa[x]==x?x:fa[x]=Find(fa[x]);
}
cdoj203-Islands 【并查集】的更多相关文章
- hust 1385 islands 并查集+搜索
islands Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/problem/show/1385 Descri ...
- USACO环绕岛屿Surround the Islands 并查集 枚举暴力
题目描述 Farmer John has bought property in the Caribbean and is going to try to raise dairy cows on a b ...
- CDOJ 203 并查集+优先队列 好题
题目链接 Islands Time Limit: 30000/10000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) S ...
- 【LeetCode】并查集 union-find(共16题)
链接:https://leetcode.com/tag/union-find/ [128]Longest Consecutive Sequence (2018年11月22日,开始解决hard题) 给 ...
- luoguP3224 [HNOI2012]永无乡【线段树,并查集】
洞庭青草,近中秋,更无一点风色.玉鉴琼田三万顷,着我扁舟一叶.素月分辉,明河共影,表里俱澄澈.悠然心会,妙处难与君说. 应念岭表经年,孤光自照,肝胆皆冰雪.短发萧骚襟袖冷,稳泛沧溟空阔.尽挹西江,细斟 ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- 关押罪犯 and 食物链(并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- bzoj1854--并查集
这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...
随机推荐
- DRL前沿之:Benchmarking Deep Reinforcement Learning for Continuous Control
1 前言 Deep Reinforcement Learning可以说是当前深度学习领域最前沿的研究方向,研究的目标即让机器人具备决策及运动控制能力.话说人类创造的机器灵活性还远远低于某些低等生物,比 ...
- html大小写问题
js中变量名,函数,关键字都区分大小写,如var i;和var I;是两个不同的变量. css中定义的元素名称不区分大小写的. html中,标签和标签属性统一使用小写形式,固有属性也一律使用小写,自定 ...
- multiprocess模块---进程---进程队列
首先明白几个概念: 同步:做完一件事情,再做另外一件事情 异步:做一件事情的时候,可以再做另外一件事情 阻塞:recv sleep accept input recvfrom 非阻塞:没有遇见上面这 ...
- UVA-673-栈-水题
题意: 检测括号是否匹配,注意有空格 #include<stdio.h> #include<iostream> #include <strstream> #incl ...
- oracle一个用户操作多个表空间中表的问题
首先,授权给指定用户. 一个用户的默认表空间只能有一个,但是你可以试下用下面的语句为其授权在别的表空间中创建对像: alter user username quota 0||unlimited on ...
- Python--001
C 和 Python.Java.C#等 C语言: 代码编译得到 机器码 ,机器码在处理器上直接执行,每一条指令控制CPU工作 其他语言: 代码编译得到 字节码 ,虚拟机执行字节码并转换成机器码再后在处 ...
- elasticsearch 基础知识汇总
索引分片: 从策略层面,控制分片分配的选择 磁盘限额 为了保护节点数据安全,ES 会定时(cluster.info.update.interval,默认 30 秒)检查一下各节点的数据目录磁盘使用情况 ...
- IP 别名和辅助 IP 地址
https://blog.csdn.net/xiewen99/article/details/54729112?utm_source=itdadao
- leetcode235
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...
- Activity服务类-6 ManagementService服务类
一共含有17个方法 // 获取包含了Activiti数据库模式的{表名.行计数}项的映射.Map<String, Long> getTableCount();//获取诸如任务.执行之类的A ...