UVA 1665 Islands
题意:输入一个n*m矩阵,每一个格子都有一个正整数,再输入T个整数ti,对于每一个ti,输出大于ti的正整数组成多少个四连快
思路:正着做的话事实上相当于删除连通块,而假设反着做的话就相当于变成添加连通块,把格子都编号然后排序。用并查集
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const int maxq = 1e5+1;
int pre[maxn*maxn];
int Find(int x)
{
return x==pre[x]?x:pre[x]=Find(pre[x]);
}
struct Node
{
int x,y;
int val;
}nodes[1005*1005];
int mapp[maxn][maxn];
int qq[maxq];
int n,m;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
bool cmp(Node a,Node b)
{
return a.val<b.val;
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
for (int i = 0;i<n;i++)
for (int j = 0;j<m;j++)
{
scanf("%d",&mapp[i][j]);
int pos = i*m+j;
nodes[pos].x=i;
nodes[pos].y=j;
nodes[pos].val=mapp[i][j];
}
int ans = 0;
int q;
scanf("%d",&q);
for (int i = 0;i<q;i++)
scanf("%d",&qq[i]);
memset(pre,-1,sizeof(pre));
sort(nodes,nodes+n*m,cmp);
int k = n*m-1;
for (int i = q-1;i>=0;i--)
{
if (qq[i]< nodes[k].val)
{
while (k>=0 && qq[i]<nodes[k].val)
{
int pos = nodes[k].x*m+nodes[k].y;
if (!~pre[pos])
pre[pos]=pos,ans++;
for (int di=0;di<4;di++)
{
int dx = nodes[k].x+dir[di][0];
int dy = nodes[k].y+dir[di][1];
if (dx>=0 && dx<n&&dy>=0&&dy<m&&mapp[dx][dy]>qq[i])
{
int ppos = dx*m+dy;
if (~pre[ppos])
{
int u = Find(ppos);
int v = Find(pos);
if (u!=v)
pre[u]=v,ans--;
}
}
}
k--;
}
if (k<0)
{
for (;i>=0;i--)
{
qq[i]=ans;
}
break;
}
}
qq[i]=ans;
}
for (int i = 0;i<q;i++)
printf("%d ",qq[i]);
printf("\n");
}
}
Description
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 nxm 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 x 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
The input contains several test cases. The first line of the input contains a positive integer Z20,
denoting the number of test cases. ThenZ test cases follow, each conforming to the format described below.
The first line contains two numbers n and m separated by a single space, the dimensions of the island, where 1n, m1000.
Next nlines 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(1T105).
The last line contains T integers tj, separated by single spaces, such that 0t1t2...tT-1tT109.
Output
For each test case, your program has to write an output conforming to the format described below.
Your program should output a single line consisting of T numbers rj separated by single spaces, where rj is the number of unflooded
areas in year tj.
cid=113332" style="color:blue; text-decoration:none">Sample Input
1
4 5
1 2 3 3 1
1 3 2 2 1
2 1 3 4 3
1 2 2 2 2
5
1 2 3 4 5
cid=113332" style="color:blue; text-decoration:none">Sample Output
2 3 1 0 0
UVA 1665 Islands的更多相关文章
- 紫书 习题 11-12 UVa 1665 (并查集维护联通分量)
这道题要逆向思维 反过来从大到小枚举, 就是在矩阵中一点一点加进去数字,这样比较 好操作, 如果正着做就要一点一点删除数字, 不好做. 我们需要在这个过程中维护联通块的个数, 这里用到了并查集. 首先 ...
- UVA 572 油田连通块-并查集解决
题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...
- [LeetCode] Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LeetCode] Number of Islands 岛屿的数量
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- UVA计数方法练习[3]
UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...
随机推荐
- User namespace
uid和gid其实很简单,主要是为了填充文件的uid和gid,这些都是静态的,那么用户执行程序这又是什么意思呢?那么进程的权限又是指什么呢? http://blog.51cto.com/skypegn ...
- 洛谷 P4008 [NOI2003]文本编辑器 解题报告
P4008 [NOI2003]文本编辑器 题目描述 很久很久以前,\(DOS3.x\)的程序员们开始对 \(EDLIN\) 感到厌倦.于是,人们开始纷纷改用自己写的文本编辑器⋯⋯ 多年之后,出于偶然的 ...
- Spring 3 MVC深入研究
一.前言: 大家好,Spring3 MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来越多的团队选择了Spring3 MVC了.Spring3 MVC结构简单,应了那句话简单就是美,而 ...
- java.security.InvalidKeyException: IOException : Short read of DER length
今天支付服务器测试退款的时候爆了异常:Caused by: java.security.InvalidKeyException: IOException : Short read of DER len ...
- 飞思卡尔MC9S12系列单片机地址影射以及分页问题
对于用MCU的人来说,不一定要明白HCS12(x) memory map的机制和联系.因为如果没有系统地学习操作系统和编译原理之类的课程,确实有些难度.并且,对于DG128 XS128这样的MCU,默 ...
- ssh xshell 连接在vim中无法用 ctrl+insert 复制黏贴
在用户目录编辑.vimrc文件不存在则创建,vi的三种模式:命令模式,插入模式,可视模式,鼠标可以启动于各种模式中,所以配置文件中的set mouse=a启动了所有模式,这样就屏蔽了鼠标右健功能,se ...
- BEE网站
http://www.bee-framework.com/ http://syxiaqj.github.io/2014/02/28/bee-learning-1/#0-tsina-1-24637-39 ...
- 使用 Python 开始你的机器学习之旅【转】
转自:https://linux.cn/article-8582-1.html 编译自:https://opensource.com/article/17/5/python-machine-learn ...
- flask框架下的jinja2模板引擎(3)(模板继承与可以在模板使用的变量、方法)
flask 框架下的jinja2模块引擎(1):https://www.cnblogs.com/chichung/p/9774556.html flask 框架下的jinja2模块引擎(2):http ...
- Informix 启动 Fatal error in shared memory initialization解决方法
https://blog.csdn.net/cy309173854/article/details/54929735