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 ...
随机推荐
- HTTP常见的状态码
状态码的职责是当客户端向服务器端发送请求时,描述返回请求结果.借助状态码,用户可以知道服务器端是正常处理了请求,还是出现了什么错误.RFC2616定义的状态码,由3位数字和原因短信组成.数字中的第一位 ...
- 关于Store Apps
因为时代在变迁,Store Apps这个概念很容易引起混淆 在过去,windows phone 8.0时代 windows store apps指的是windows metro style 的应用, ...
- windows phone因为墓碑化导致“正在恢复”的分析
我们在平时的WP使用过程中,会遇到一个问题 应用在切出,切回后, 有时候,会显示"正在恢复",并等待时间较长,才能回到用户切出时候的画面,但是这种情况并非常见,偶尔发生 有时候,直 ...
- 12 个 Linux 进程管理命令介绍
执行中的程序在称作进程.当程序以可执行文件存放在存储中,并且运行的时候,每个进程会被动态得分配系统资源.内存.安全属性和与之相关的状态.可以有多个进程关联到同一个程序,并同时执行不会互相干扰.操作系统 ...
- webService 客户端接口调用【java】
最近实际项目中使用到了WebService,简单总结下使用方式: 1.拿到接口:http://*******:8080/osms/services/OrderWebService?wsdl 我们可以将 ...
- [每日一题] OCP1z0-047 :2013-08-17 EXTERNAL TABLE――加载数据 ............................56
正确答案:C 一.对答案解释: A. TYPE:有两个选可供选择: 1. ORACLE_LOADER:传统方式,与SQLLDR一样,参数从多,应用较多. 2. ...
- [Ionic] Build and Run an Ionic App from Scratch
Install: npm install ionic cordova -g Create a project with blank template: ionic start <project_ ...
- boost 无锁队列
一哥们翻译的boost的无锁队列的官方文档 原文地址:http://blog.csdn.net/great3779/article/details/8765103 Boost_1_53_0终于迎来了久 ...
- sql语句中查询出的数据添加一列,并且添加默认值
查询出数据,并且要添加一列表中都不存在的数据,且这一列的值都是相等的 select app_id,app_secret from wx_ticket group by app_id; 查询出的数据是 ...
- Makefile 工程管理
Makefile 工程管理 Makefile 规则 --变量 在Makefile中,用户除了可以自己定义变量外,还可以使用存在系统已经定义好的默认变量 $^:代表所有的依赖文件 $@:代表目标 $&l ...