islands

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/problem/show/1385

Description

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 x 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 4x5 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

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

Sample Output

2 3 1 0 0

HINT

题意

一个n*m的图上,每一个区域都有一个权值,然后每个权值

然后有k个查询,给你n个d[i]
每次都会让大于d[i]的点起来,然后问你每次分别有多少个联通分量

题解:

首先我们离线做,然后我们排序,从大到小弄

这样子我们就可以很轻松的想到一个
类似动态转移一样的方法
然后我们再拿并查集维护一下,再搞一搞就好了

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1001
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff; //无限大
const int inf=0x3f3f3f3f;
/*
inline ll read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int buf[10];
inline void write(int i) {
int p = 0;if(i == 0) p++;
else while(i) {buf[p++] = i % 10;i /= 10;}
for(int j = p-1; j >=0; j--) putchar('0' + buf[j]);
printf("\n");
}
*/
//************************************************************************************** int fa[maxn*maxn],ans,n,m,k,tot,q[maxn*maxn],vis[maxn][maxn];
int g[maxn][maxn];
struct node
{
int x,y,z;
};
bool cmp2(int a,int b)
{
return a>b;
}
bool cmp(node a,node b)
{
return a.z>b.z;
}
node a[maxn*maxn];
int id(int x,int y)
{
return *x+y;
}
int fi(int x)
{
return x == fa[x] ? x : fa[x] = fi(fa[x]);
} void un(int a, int b)
{
a = fi(a);
b = fi(b);
if(a == b)
{
return;
}
fa[b] = a;
ans--;
}
int judge(int x,int y)
{
if(x>= && x<n && y>= && y<m)
if(vis[x][y])
return ;
return ;
}
int dx[]={,-,,};
int dy[]={,,,-};
void init()
{
memset(fa,,sizeof(fa));
memset(a,,sizeof(a));
ans=;
memset(q,,sizeof(q));
memset(vis,,sizeof(vis));
memset(g,,sizeof(g));
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d%d",&n,&m);
tot=;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
scanf("%d",&a[tot].z);
a[tot].x=i;
a[tot++].y=j;
fa[id(i,j)]=id(i,j);
}
}
sort(a,a+tot,cmp);
scanf("%d",&k);
for(int i=;i<k;i++)
scanf("%d",&q[i]);
sort(q,q+k,cmp2);
vector<int> qq;
int pic=;
for(int i=;i<k;i++)
{
while(pic<tot&&a[pic].z>q[i])
{
ans++;
int x=a[pic].x,y=a[pic].y;
vis[x][y]=;
for(int j=;j<;j++)
{
int xx=x+dx[j],yy=y+dy[j];
if(judge(xx,yy))
{
un(id(x,y),id(xx,yy));
}
}
pic++;
}
qq.push_back(ans);
}
for(int i=qq.size()-;i>=;i--)
{
printf("%d ",qq[i]);
}
printf("\n");
}
}

最新文章

  1. C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 多软件系统集成缓存体系改进
  2. JS 做的鼠标放大镜(初级)
  3. nfs文件系统挂载错误及解决方法
  4. Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds 解决方法
  5. 对象化的Http和请求对象HttpRequest
  6. 参数db_ultra_safe
  7. BZOJ 2243 SDOI 2011染色
  8. Git 笔记三 Git的初步使用
  9. js显示时间
  10. [lua]luasocket.c:20:17: fatal error: lua.h: No such file or directory
  11. 服务端搭建——腾讯云通信(IM)
  12. requests使用retry策略
  13. flask框架----flask基础
  14. 【Python】unittest-2-断言
  15. unfolding maps支持中文
  16. Ajax和SpringMVC之间JSON交互
  17. [i.MX6q]i.MX6q处理器,linux操作系统平台搭建 从SD卡启动系统
  18. Css三栏布局自适应实现几种方法
  19. oracle之 利用 controlfile trace文件重建控制文件
  20. 【后缀自动机】hdu6194 string string string

热门文章

  1. IE9 下 ellipsis bug fix
  2. SSO单点登录的发展由来以及实现原理【转】
  3. jquery 通过 live() 方法附加的事件处理程序适用于匹配选择器的当前及未来的元素(比如由脚本创建的新元素)
  4. angular项目中使用Primeng
  5. mysql视图学习总结(转)
  6. python datetime 时区(timezone) dateutil
  7. explicit浅谈
  8. vs2013设置语言
  9. css 字符图标浏览器自带
  10. QT构造函数中*parent