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 ...
随机推荐
- poj2196
Specialized Four-Digit Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7238 A ...
- 初探JS正则表达式
1.概述 正则表达式是一个描述字符模式的对象.Javascript的正则表达式语法的是Perl5的正则表达式的子集.JS正则表达式有两种使用方式,文本模式和RegExp对象模式,实例如下: v ...
- flex渐变色制作圆角橙色按钮
<?xml version="1.0" encoding="utf-8"?> <s:SparkButtonSkin xmlns:fx=&quo ...
- 一个sql很多个not like的简化语句
如: select * from table where `zongbu` not like '%北京%' and `zongbu` not like '%上海%' and `zongbu` not ...
- 十分钟学会写shell脚本
大家好!我是handsomecui,下面我为大家讲解一下shell脚本的写法,讲的不好的地方,欢迎大家留言拍砖. 1.在linux下会写shell脚本是非常重要的,下面我参照例子给大家展示几个脚本,顺 ...
- C#软件开发实例.私人订制自己的屏幕截图工具(七)加入放大镜的功能
上一篇:C#软件开发实例.私人订制自己的屏幕截图工具(六)加入配置管理功能 因为截图时可能须要精确截取某一部分,所以须要放大镜的功能,这样截取的时候才更easy定位截图的位置. 加入PictureBo ...
- iOS新的旅程之Swift语言的学习
好久都没有来这个熟悉而又陌生的地方啦, 想想已经有两三个月了吧,不过我相信以后还是会经常来的啦,因为忙碌的学习已经过去啦,剩下的就是要好好的总结好好的复习了,好好的熟悉下我们之前学习的知识点,将他们有 ...
- MongoDB学习笔记04
创建索引使用ensureIndex方法,对于同一个集合,同样的索引只需要创建一次,反复创建是徒劳的. 对某个键的索引会加速对该键的查询,然而,对于其它查询可能没有帮助,即便是查询中包含了被索引的键.实 ...
- varchar和Nvarchar的区别
(1)varchar(N) 存储时 N的单位是字节 比如说varchar(2) 代表的是 该字段可以存储字节长度为2的数据 例子:可以添加 张 或者 ab 添加成功! 但添加的时候如果是: ...
- PHP学习笔记二十九【接口】
<?php //定义接口 //接口可以定义属性,但必须是常量而且是public //接口的所有方法必须是public interface Iusb{ public function start( ...