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.

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 ;
}

UESTC_Islands 2015 UESTC Training for Data Structures<Problem J>的更多相关文章

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

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

  3. UESTC_秋实大哥与战争 2015 UESTC Training for Data Structures<Problem D>

    D - 秋实大哥与战争 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Subm ...

  4. UESTC_秋实大哥与快餐店 2015 UESTC Training for Data Structures<Problem C>

    C - 秋实大哥与快餐店 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

  5. UESTC_秋实大哥搞算数 2015 UESTC Training for Data Structures<Problem N>

    N - 秋实大哥搞算数 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Subm ...

  6. UESTC_秋实大哥与线段树 2015 UESTC Training for Data Structures<Problem M>

    M - 秋实大哥与线段树 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

  7. UESTC_秋实大哥下棋 2015 UESTC Training for Data Structures<Problem I>

    I - 秋实大哥下棋 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  8. UESTC_秋实大哥打游戏 2015 UESTC Training for Data Structures<Problem H>

    H - 秋实大哥打游戏 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Subm ...

  9. UESTC_秋实大哥去打工 2015 UESTC Training for Data Structures<Problem G>

    G - 秋实大哥去打工 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Subm ...

随机推荐

  1. 04747_Java语言程序设计(一)_第6章_图形界面设计(二)

    例6.1声明一个面板子类,面板子类对象有3个选择框. class Panel1 extends JPanel { JCheckBox box1, box2, box3; Panel1() { box1 ...

  2. struts——拦截器

    什么是拦截器 拦截器(Interceptor)是Struts 2的一个强有力的工具,有许多功能都是构建于它之上,如国际化(前两篇博客介绍过).转换器,校验等. 拦截器是动态拦截Action调用的对象. ...

  3. 十分钟学会写shell脚本

    大家好!我是handsomecui,下面我为大家讲解一下shell脚本的写法,讲的不好的地方,欢迎大家留言拍砖. 1.在linux下会写shell脚本是非常重要的,下面我参照例子给大家展示几个脚本,顺 ...

  4. (3)选择元素——(6)属性选择器(Attribute selectors)

    Attribute selectors are a particularly helpful subset of CSS selectors. They allow us to specify an ...

  5. web项目的两个创建形式website和webapplication

    前言 在利用VS2010创建web项目的时候,会有两个选择.可以选择直接创建website网站,还可以选择使用 webapplication应用程序.刚刚接触web开发,看到这两个就疑惑了,既然是都可 ...

  6. 01我为什么学Unity3d

    首发于游戏蛮牛论坛&&我的CSDN博客:http://blog.csdn.net/wowkk/article/details/18571055 转载请说明出处.谢谢. 本人现大学生,带 ...

  7. Guava源码分析——ServiceManager

    ServiceManager类:      用于监控服务集的管理器,该类提供了诸如startAsync.stopAsync.servicesByState方法来运行.结束和检查服务集,而且,通过监听器 ...

  8. [汇编学习笔记][第十七章使用BIOS进行键盘输入和磁盘读写

    第十七章 使用BIOS进行键盘输入和磁盘读写 17.1 int 9 中断例程对键盘输入的处理 17.2 int 16 读取键盘缓存区 mov ah,0 int 16h 结果:(ah)=扫描码,(al) ...

  9. 《JavaScript 闯关记》之基本包装类型

    为了便于操作基本类型值,JavaScript 还提供了3个特殊的引用类型:Boolean.Number 和 String.实际上,每当读取一个基本类型值的时候,后台就会创建一个对应的基本包装类型的对象 ...

  10. Oracle/Mysql/SqlServer函数区别

    mysql日期和时间格式转换 Linux scp 使用详解 Oracle/Mysql/SqlServer函数区别 2011-07-01 12:34:36|  分类: Mysql技术 |  标签:mys ...