UESTC_Islands 2015 UESTC Training for Data Structures<Problem J>
J - 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 <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 ;
}
UESTC_Islands 2015 UESTC Training for Data Structures<Problem J>的更多相关文章
- UESTC_Rain in ACStar 2015 UESTC Training for Data Structures<Problem L>
L - Rain in ACStar Time Limit: 9000/3000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Other ...
- UESTC_Sliding Window 2015 UESTC Training for Data Structures<Problem K>
K - Sliding Window Time Limit: 18000/6000MS (Java/Others) Memory Limit: 131072/131072KB (Java/Ot ...
- UESTC_秋实大哥与战争 2015 UESTC Training for Data Structures<Problem D>
D - 秋实大哥与战争 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥与快餐店 2015 UESTC Training for Data Structures<Problem C>
C - 秋实大哥与快餐店 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
- UESTC_秋实大哥搞算数 2015 UESTC Training for Data Structures<Problem N>
N - 秋实大哥搞算数 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥与线段树 2015 UESTC Training for Data Structures<Problem M>
M - 秋实大哥与线段树 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
- UESTC_秋实大哥下棋 2015 UESTC Training for Data Structures<Problem I>
I - 秋实大哥下棋 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- UESTC_秋实大哥打游戏 2015 UESTC Training for Data Structures<Problem H>
H - 秋实大哥打游戏 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥去打工 2015 UESTC Training for Data Structures<Problem G>
G - 秋实大哥去打工 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
随机推荐
- SEO的URL如何优化才是最佳
原文地址:http://www.chinaz.com/web/2007/0413/6841.shtml 很多人都知道URL对SEO的重要之处,但是很多站点却忽略了站点的路径优化.今天本人在这里写几点关 ...
- (DP)Best Time to Buy and Sell Stock
题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...
- poj 2411 Mondriaan's Dream_状态压缩dp
题意:给我们1*2的骨牌,问我们一个n*m的棋盘有多少种放满的方案. 思路: 状态压缩不懂看,http://blog.csdn.net/neng18/article/details/18425765 ...
- 【HDU1232】畅通工程(并查集基础题)
裸敲并查集,很水一次AC #include <iostream> #include <cstring> #include <cstdlib> #include &l ...
- OpenStack Mixture HypervisorsDriver configure and implementation theory
通过本文,您将可以了解在 OpenStack 中如何进行混合 Hypervisor 的配置及其实现原理的基本分析.本文主要结合作者在 Nova 中的实际开发经验对 OpenStack 中混合 Hype ...
- web应用,我们需要了解什么?
对于前端开发来说,web应用我们并不陌生.今天想要讨论一下,在开发一个web应用的时候,我们需要一些基本的知识储备.我们知道,一个web应用脱离不了(request)请求和响应(respons ...
- xcode5下面安装Command Line Tools
运行命令 sudo xcode-select --install 就会显示一行文字,并且弹出一个对话框,确认安装后他就会自己下载来安装了. 至此,Command Line Tools就能够重新复活了
- Struts2+Spring+Hibernate 三大框架的合并集成
这次来看看Struts2+Spring+Hibernate三大框架的整合应用,主要是Spring和Hibernate框架的整合,因为前边已经将Strtus2+Spring整合过了基本一样. 首先看一 ...
- UISegmentedControl判断点击第几项
UISegmentedControl 关于UISegmentedControl判断当前点击的是第几项,找了很久,终于再老外的博客上找到了,在委托中 UISegmentedControl *Seg=se ...
- 关于asp:login控件和验证码的问题?(转)
1.验证码页面添加.2.将这验证码页面添加到login控件中:拖曳一Login控件,将之切换到模式下,在Html源文件中在表格中密码那行后添加: <tr> <td style= ...