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

Download as PDF

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 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.

\epsfbox{p4627.eps}

Input 

The input contains several test cases. The first line of the input contains a positive integer  Z$ \le$20, denoting the number of test cases. Then Z 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 1$ \le$nm$ \le$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(1$ \le$T$ \le$105). The last line contains T integers tj, separated by single spaces, such that 0$ \le$t1$ \le$t2$ \le$...$ \le$tT-1$ \le$tT$ \le$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.

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

Sample Output 

2 3 1 0 0


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值