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

hust 1385 islands 并查集+搜索的更多相关文章

  1. 2018.09.24 bzoj1016: [JSOI2008]最小生成树计数(并查集+搜索)

    传送门 正解是并查集+矩阵树定理. 但由于数据范围小搜索也可以过. 我们需要知道最小生成树的两个性质: 不同的最小生成树中,每种权值的边出现的个数是确定的 不同的生成树中,某一种权值的边连接完成后,形 ...

  2. USACO环绕岛屿Surround the Islands 并查集 枚举暴力

    题目描述 Farmer John has bought property in the Caribbean and is going to try to raise dairy cows on a b ...

  3. zoj 3761(并查集+搜索)

    题意:在一个平面上,有若干个球,给出球的坐标,每次可以将一个球朝另一个球打过去(只有上下左右),碰到下一个球之后原先的球停下来,然后被撞的球朝这个方向移动,直到有一个球再也撞不到下一个球后,这个球飞出 ...

  4. 【搜索】【并查集】Codeforces 691D Swaps in Permutation

    题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...

  5. Stanford Local 2016 E "Election of Evil"(搜索(正解)或并查集(划掉))

    传送门 题意: 给出集合U,V,集合U有n个元素,集合V有m个元素: 有 m 个操作,mi : s1 s2 有一条s1指向s2的边(s1,s2可能属于第三个集合,暂且称之为K集合): 指向边具有传递性 ...

  6. HDU3926Hand in Hand(搜索 或 并查集)

    Problem Description In order to get rid of Conan, Kaitou KID disguises himself as a teacher in the k ...

  7. 【NOIP模拟_54测试】【并查集】【二进制】【搜索】【区间序列类】

    第一题 Mushroom的序列 大意: 给一个序列,求一段连续最长区间满足:最多改变一个数,使得区间是严格的上升子序列. 解: 直接扫描一遍,记一个最长上升子序列编号.然后从每一个编号为1 的点来判断 ...

  8. HNUSTOJ-1674 水果消除(搜索或并查集)

    1674: 水果消除 时间限制: 2 Sec  内存限制: 128 MB提交: 335  解决: 164[提交][状态][讨论版] 题目描述 “水果消除”是一款手机游戏,相信大家都玩过或玩过类似的游戏 ...

  9. CDOJ 203 并查集+优先队列 好题

    题目链接 Islands Time Limit: 30000/10000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) S ...

随机推荐

  1. Coursera在线学习---第四节.过拟合问题

    一.解决过拟合问题方法 1)减少特征数量 --人为筛选 --靠模型筛选 2)正则化(Regularization) 原理:可以降低参数Θ的数量级,使一些Θ值变得非常之小.这样的目的既能保证足够的特征变 ...

  2. 面试中关于Java虚拟机(jvm)的问题看这篇就够了

    最近看书的过程中整理了一些面试题,面试题以及答案都在我的文章中有所提到,希望你能在以问题为导向的过程中掌握虚拟机的核心知识.面试毕竟是面试,核心知识我们还是要掌握的,加油~~~ 下面是按jvm虚拟机知 ...

  3. 使用迭代法穷举1到N位最大的数

    这是何海涛老师剑指offer上面第12题,这题首先注意不能使用整数int型作为操作对象,因为N很大时明显会溢出.这种大数据一般都是使用的字符串来表示. 直接法就是:1.针对字符串的加法,涉及循环进位及 ...

  4. [ python ] 初始面向对象

    首先,通过之前学习的函数编写一个 人狗大战 的例子. 分析下这个需求,人 狗 大战  三个事情.角色:人.狗动作:狗咬人,人打狗 先创建人和狗两个角色: def person(name, hp, ag ...

  5. Mybatis的关联映射案例

    主要是对之前学习的关联映射做一个案例,自己动手实践一下,可以理解的更好一点. 开发环境 开发工具:idea Java环境: jdk1.8.0_121 数据库:SQLServer 项目结构,里面包含了三 ...

  6. 关于Windows中的硬链接

    https://zhidao.baidu.com/question/748233720330351012.html linux中使用硬链接 ln a.txt b.txt 查看硬链接 ls -il 关于 ...

  7. linux中top命令

    经常用到top命令,也就简单看看进程多不多,卡不卡, 这次在网上找到一个归纳总结的,以供参考. 简介 top 命令是 Linux 下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于 ...

  8. 为啥我喜欢在Windows 7环境下做Unity开发?

    先说明,以下情况只针对本人哦~ 前阵子我在OSX的最新版本Mavericks下做Unity开发,后来我把MacbookPro卖了,自己组装了个PC搞开发,为啥呢? 1:OSX下 MonoDevelop ...

  9. Jquery 官网下载流程

    选中解压版本,然后把ctrl+s保存另存为他的min版本

  10. FastReport.Net使用:[13]如何使用表达式

    在FastReport报表中,表达式(Expressions)用在很多地方,譬如文本框,排序过滤器等. 表达式基于报表选择的脚本语言,从菜单[报表]->[选项]打开“报表选项对话框”,切换到“脚 ...