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, m
1000.
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(1T
105).
The last line contains T integers tj, separated by single spaces, such that 0t1
t2
...
tT-1
tT
109.
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 ...
随机推荐
- 洛谷 P2312 解方程 解题报告
P2312 解方程 题目描述 已知多项式方程: \(a_0+a_1x+a_2x^2+\cdots+a_nx^n=0\)求这个方程在 \([1,m]\) 内的整数解(\(n\) 和 \(m\) 均为正整 ...
- [解决方案]未能找到路径“~\bin\roslyn\csc.exe”的一部分
我的WebApi项目使用Nuget加载了一些包以后出现了这样的问题,本地可以访问,但发布到线上后,出现这样的报错 这个问题出现的原因是Nuget的时候,多加载了一些项目可能不需要的依赖库所导致的. 解 ...
- font-family 定义的最后为什么要加一句sans-serif
定义font-family时,最好在最后加一个sans-serif,这样如果所列出的字体都不能用,则默认的sans-serif字体能保证调用; W3C建议字体定义的时候,最后以一个类别的字体结束,例如 ...
- forEach循环dom元素
//让ie8支持foreach if (typeof Array.prototype.forEach != 'function') { Array.prototype.forEach = functi ...
- 经典linux书籍
入门篇<LINUX权威指南>书不错,写的很全面也比较广,涉及的不深,做为入门书籍不错,可以比较全面的了解linux .另外比较热门的也可以看看<鸟哥的私房菜>等书,偏管理类的书 ...
- jquery.uploadify不支持MVC的Authorize
原文发布时间为:2011-10-18 -- 来源于本人的百度文章 [由搬家工具导入] 为什么jquery.uploadify不支持MVC的Authorize呢,因为flash的cookie跟服务端的不 ...
- Linux应用层的定时器Timer使用详解【转】
转自:http://blog.csdn.net/wwwtovvv/article/details/8601528 版权声明:本文为博主原创文章,未经博主允许不得转载. linux下定时器的使用 -- ...
- SQLite 字段数据类型
一般数据采用的固定的静态数据类型,而SQLite采用的是动态数据类型,会根据存入值自动判断. SQLite具有以下五种数据类型: 1.NULL:空值. 2.INTEGER:带符号的整型,具体取决有存入 ...
- 用户空间缺页异常pte_handle_fault()分析--(上)【转】
转自:http://blog.csdn.net/vanbreaker/article/details/7881206 版权声明:本文为博主原创文章,未经博主允许不得转载. 前面简单的分析了内核处理用户 ...
- 集合类---List
一.ArrayList详解 1.继承关系 public class ArrayList<E> extends AbstractList<E> implements List&l ...