UESTC_Islands 2015 UESTC Training for Data Structures

J - Islands

Time Limit: 30000/10000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit Status

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.

UESTC_Islands 2015 UESTC Training for Data Structures<Problem J>

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
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
2 3 1 0 0

解题报告

注意到随着时间的增多,原先的集合会分裂,那么倒着来看,集合则会合并!,倒序维护所有时间询问即可,采用并查集来实现集合的合并,注意合并的几种情况(对整体集合数量的增减),即可解决本题.

 #include <iostream>
#include <cstring>
#include <set>
using namespace std;
const int maxn = 1e6 + ;
int pre[maxn],n,m,querysize;
int query[maxn],ans[maxn];
int dir[][] = {,,,-,-,,,};
bool colour[][]; inline int getid(int x,int y)
{
return x*m+y;
} typedef struct Area
{
int x,y,h;
friend bool operator < (const Area&a,const Area& b)
{
return a.h < b.h;
}
Area(const int &x,const int &y,const int &h)
{
this->x = x ,this-> y = y , this->h = h;
}
}; multiset<Area>s; int getfather(int cur)
{
if (pre[cur] == cur)
return cur;
else
return pre[cur] = getfather(pre[cur]);
} int union_(int tx,int ty)
{
int res = ;
int id = getid(tx,ty);
int data[],datasize = ;
for(int i = ; i < ; ++ i)
{
int newx = tx + dir[i][];
int newy = ty + dir[i][];
if (newx < || newx >= n || newy < || newy >= m)
continue;
if (colour[newx][newy])
data[datasize++] = getid(newx,newy);
}
colour[tx][ty] = true;
if (!datasize)
return ;
else
{
int res = ;
pre[getfather(id)] = getfather(data[]);
for(int i = ; i < datasize ; ++ i)
{
if (getfather(data[i]) != getfather(data[]))
{
pre[getfather(data[i])] = getfather(data[]);
res++;
}
}
return -res;
}
} int main(int argc,char *argv[])
{
int Case;
scanf("%d",&Case);
while(Case--)
{
s.clear();
memset(colour,false,sizeof(colour));
scanf("%d%d",&n,&m);
for(int i = ; i < n ; ++ i)
for(int j = ; j < m ; ++ j)
{
int temp;
scanf("%d",&temp);
s.insert(Area(i,j,temp));
}
for(int i = ; i < n*m ; ++ i)
pre[i] = i;
scanf("%d",&querysize);
for(int i = ; i < querysize ; ++ i)
scanf("%d",&query[i]);
int curans = ;
for(int i = querysize- ; i >= ; -- i)
{
int h = query[i];
set<Area>::iterator it = s.upper_bound(Area(,,h));
while(it != s.end())
{
int tx = it->x , ty = it->y;
curans += union_(tx,ty);
s.erase(it++);
}
ans[i] = curans;
}
for(int i = ; i < querysize ; ++ i)
printf("%d ",ans[i]);
printf("\n");
}
return ;
}
上一篇:winform:简单文件资源管理器


下一篇:102.二叉树的层序遍历