题目传送门

pow

题意翻译

Description 你手头有一张该市的地图。这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形。对于每个小正方形,地图上已经标注了它的海拔高度以及它是否是该市的一个组成部分。地图上的所有部分都被水淹没了。并且,由于这张地图描绘的地面周围都被高山所环绕,洪水不可能自动向外排出。显然,我们没有必要抽干那些非该市的区域。

每个巨型抽水机可以被放在任何一个1∗1正方形上。这些巨型抽水机将持续地抽水直到这个正方形区域里的水被彻底抽干为止。当然,由连通器原理,所有能向这个格子溢水的格子要么被抽干,要么水位被降低。每个格子能够向相邻的格子溢水,“相邻的”是指(在同一高度水平面上的射影)有公共边。

Input

第一行是两个数m,n(1<=m,n<=1000).

以下 m 行,每行 n 个数,其绝对值表示相应格子的海拔高度;若该数为正,表示它是该市的一个区域;否则就不是。

请大家注意:所有格子的海拔高度其绝对值不超过 1000 ,且可以为零.

Output

只有一行,包含一个整数,表示至少需要放置的巨型抽水机数目。

感谢@FlashHu 提供的翻译

题目描述

Byteburg, the capital of Byteotia, is a picturesque city situated in a valley in the midst of mountains. Unfortunately, recent heavy rainfall has caused a flood - all the Byteburg is now completely under water. Byteasar, the king of Byteotia, has summoned his most enlightened advisors, including you, to a council. After long deliberations the council agreed to bring a few pumps, set them up in the flooded area and drain Byteburg.

The king has asked you to determine the minimum number of pumps sufficing to drain the city.

You are provided with a map of the city and the valley it is situated in. The map is in the shape of a m\times nm×nrectangle, divided into unitary squares. For each such square the map tells its height above sea level and alsowhether it is a part of Byteburg or not. The whole area depicted in the map is under water. Furthermore, it issurrounded by much higher mountains, making the outflow of water impossible. Obviously, there is no needto drain the area that does not belong to Byteburg.

Each pump can be placed in any unitary square depicted in the map. The pump will be drawing thewater until its square is completely drained. Of course, the communicating tubes principle makes its work, so draining one square results in lowering the water level or complete draining of those squares from which the water can flow down to the one with the pump. Water can flow only between squares with a common side (or, more exact, squares whose projections onto horizontal plane have a common side, since the squares may be at different level). Apart from that, the water obviously only flows down.

Task

Write a programme that:

  • reads description of the map from the standard input,

  • determines the minimum number of pumps needed to drain whole Byteburg,

  • writes out the outcome to the standard output.

给定一张地势图,所有的点都被水淹没,现在有一些关键点,要求放最少的水泵使所有关键点的水都被抽干

输入输出格式

输入格式:

In the first line of the standard input there are two integers m and n , separated by a single space,1≤n,m≤1 000 . The following mm lines contain the description of the map. The (i+1) 'th line describes the i 'th row of unitary squares in the map. It contains nn integers xi,1​,xi,2​,...,xin​​ , separated by single spaces, −1 000≤xi,j​≤1 000 , xi,j​≠1000 . The number xi,j​ describes the j 'th square of theii 'th line. The ground level in this square is ∣xi,j​∣ above sea level. If xi,j​>0 , then the square is part of Byteburg, otherwise it is outside the city. Notice, that the area of Byteburg need not be connected. In fact the city may have several separate parts.

输出格式:

Your programme should write out one integer to the standard output - the minimum number of pumpsneeded to drain Byteburg.

输入输出样例

输入样例#1:

  1. 6 9
  2. -2 -2 -1 -1 -2 -2 -2 -12 -3
  3. -2 1 -1 2 -8 -12 2 -12 -12
  4. -5 3 1 1 -12 4 -6 2 -2
  5. -5 -2 -2 2 -12 -3 4 -3 -1
  6. -5 -6 -2 2 -12 5 6 2 -1
  7. -4 -8 -8 -10 -12 -8 -6 -6 -4

输出样例#1:

2


  分析:

  比较考验思维的一道并查集题。

  首先,我们可以将每个点用一个值表示(用两个坐标表示不太方便),然后将其按照海拔从小到大排序,然后枚举每一个点,如果相邻的点的海拔$\leq$该点的海拔,则水可以又该点流向相邻的点,那么就将他们放在同一个并查集里。然后判断该店是否为城市,如果是则需要在联通块内放一个抽水机,如果不是则不用。但是注意,一定要将同一高度的点枚举完之后才能判断,否则会被特殊点卡掉。具体怎么特殊蒟蒻就不多讲,相信各位大佬们一定想得到。具体看代码吧。

  Code:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N=1e3+;
  4. int n,m,a[N][N],cnt;
  5. bool vis[N*N];
  6. int fa[N*N],num[N][N],ans;
  7. int mvx[]={,,,-};
  8. int mvy[]={,-,,};
  9. struct Node{
  10. int x,y,val;
  11. bool operator < (const Node &x) const {
  12. return val<x.val;}
  13. }q[N*N];
  14. inline int read()
  15. {
  16. char ch=getchar();int num=;bool flag=false;
  17. while(ch<''||ch>''){if(ch=='-')flag=true;ch=getchar();}
  18. while(ch>=''&&ch<=''){num=num*+ch-'';ch=getchar();}
  19. return flag?-num:num;
  20. }
  21. inline int find(int x)
  22. {
  23. return fa[x]==x?x:fa[x]=find(fa[x]);
  24. }
  25. inline int Abs(int x)
  26. {
  27. return x>?x:-x;
  28. }
  29. inline void merge(int x,int y)
  30. {
  31. int fax=find(x),fay=find(y);
  32. vis[fay]|=vis[fax];
  33. fa[fax]=fay;
  34. }
  35. int main()
  36. {
  37. m=read();n=read();
  38. memset(vis,,sizeof(vis));
  39. memset(a,,sizeof(a));
  40. for(int i=;i<=m;i++)
  41. for(int j=;j<=n;j++){
  42. a[i][j]=read();}
  43. for(int i=;i<=m;i++)
  44. for(int j=;j<=n;j++){
  45. num[i][j]=++cnt;
  46. q[cnt]=(Node){i,j,Abs(a[i][j])};}
  47. sort(q+,q+cnt+);
  48. for(int i=;i<=cnt;i++)fa[i]=i;
  49. for(int i=;i<=cnt;i++){
  50. int X=q[i].x,Y=q[i].y;
  51. for(int j=;j<;j++){
  52. int x=X+mvx[j],y=Y+mvy[j];
  53. if(Abs(a[x][y])<=q[i].val){
  54. merge(num[x][y],num[X][Y]);
  55. }
  56. }
  57. if(q[i].val!=q[i+].val){
  58. for(int j=i;q[j].val==q[i].val;j--)
  59. if(a[q[j].x][q[j].y]>){
  60. int fax=find(num[q[j].x][q[j].y]);
  61. if(!vis[fax])vis[fax]=,ans++;
  62. }
  63. }
  64. }
  65. printf("%d\n",ans);
  66. return ;
  67. }

洛谷P3457 [POI2007]POW-The Flood [并查集,模拟]的更多相关文章

  1. 洛谷P3247 [HNOI2016]最小公倍数 [分块,并查集]

    洛谷 思路 显然,为了达到这个最小公倍数,只能走\(a,b\)不是很大的边. 即,当前询问的是\(A,B\),那么我们只能走\(a\leq A,b\leq B\)的边. 然而,为了达到这最小公倍数,又 ...

  2. Bzoj5188/洛谷P4185 [Usaco2018 Jan]MooTube(并查集)

    题面 Bzoj 洛谷 题解 最暴力的方法是直接判两个点之间的路径最小值是否\(\geq k\),用\(Dijkstra\)可以做到该算法最快效率,但是空间复杂度始终是\(O(n^2)\)的,会\(ML ...

  3. 洛谷P4004 Hello world!(分块+并查集)

    传送门 虽然洛谷数据水,然而咱最终还是没有卡过uoj上的毒瘤数据-- 神tm全uoj就3个人过了这题-- 首先,每个数最多被开根\(6\)次,开到\(1\)之后就别管它了,把它用并查集连到它父亲上 它 ...

  4. 洛谷P1196 银河英雄传说[带权并查集]

    题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山 ...

  5. [洛谷P1197/BZOJ1015][JSOI2008]星球大战Starwar - 并查集,离线,联通块

    Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...

  6. 【洛谷P3224】永无乡 并查集+Splay启发式合并

    题目大意:给定 N 个点的图,点有点权,初始有一些无向边,现在有 Q 个询问,每个询问支持动态增加一条无向边连接两个不连通的点和查询第 X 个点所在的联通块中权值第 K 大的是哪个点. 题解:学会了平 ...

  7. 洛谷P4768 [NOI2018]归程 [可持久化并查集,Dijkstra]

    题目传送门 归程 格式难调,题面就不放了. 分析: 之前同步赛的时候反正就一脸懵逼,然后场场暴力大战,现在呢,还是不会$Kruskal$重构树,于是就拿可持久化并查集做. 但是之前做可持久化并查集的时 ...

  8. 【洛谷】P1196 银河英雄传说(并查集)

    题目描述 公元五八○一年,地球居民迁至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山压顶 ...

  9. 洛谷 - P1552 - 派遣 - 左偏树 - 并查集

    首先把这个树建出来,然后每一次操作,只能选中一棵子树.对于树根,他的领导力水平是确定的,然后他更新答案的情况就是把他子树内薪水最少的若干个弄出来. 问题在于怎么知道一棵子树内薪水最少的若干个分别是谁. ...

随机推荐

  1. 【Java-GUI】homework~QQ登录界面

    话说有图有真相:(图片文件自己ps吧,动态网页未添加成功,后附html源码) Java源码: import javax.swing.*; import java.awt.*; import java. ...

  2. css单位em、px、rem和pt的区别

    1.PX :像素(Pixel) PX是相对长度单位,它是相对于显示器屏幕分辨率而言的. 优缺点:比较稳定和精确,但在浏览器中放大或缩放浏览页面时会出现页面混乱的情况. 2.EM:是相对长度单位. EM ...

  3. JQuery和Servlet来实现跨域请求

    在网上看到很多的JQuery跨域请求的文章,比较有意思.这里我发表一个Servlet与JQuery配置实现跨域的代码,供大家参考.不足之处请指教 原理:JavaScript的Ajax不可以跨域,但是可 ...

  4. MyBatis数据库字段和实体对象属性名不一致的解决方案

    数据库和对象的属性名不一致是很常见的问题,这个时候依从表字段到对象属性名的按名称匹配映射已经搞不定这个了,下面是几种解决方案. 1. 开启驼峰转换 如果数据库中的字段名与对象只是简单的不一致的话,比如 ...

  5. NB二人组(一)----堆排序

    堆排序前传--树与二叉树简介 特殊且常用的树--二叉树  两种特殊的二叉树 二叉树的存储方式 二叉树小结 堆排序 堆这个玩意....... 堆排序过程: 构造堆: 堆排序的算法程序(程序需配合着下图理 ...

  6. jquery 生成二维码

    jquery的二维码生成插件qrcode,在页面中调用该插件就能生成对应的二维码 <!DOCTYPE html> <html> <head> <meta ch ...

  7. python简单爬虫一

    简单的说,爬虫的意思就是根据url访问请求,然后对返回的数据进行提取,获取对自己有用的信息.然后我们可以将这些有用的信息保存到数据库或者保存到文件中.如果我们手工一个一个访问提取非常慢,所以我们需要编 ...

  8. linux设置时区同步时间

    linux设置时区同步时间 一.运行tzselect sudo tzselect 在这里我们选择亚洲 Asia,确认之后选择中国(China),最后选择北京(Beijing) 如图:   二.复制文件 ...

  9. Python 生成随机数

    import random x = int(input('Enter a number for x: '))  --随机数最小值y = int(input('Enter a number for y: ...

  10. javascript你不知道的This

    <你不知道的javascript>这本书读了有好几遍了,似乎每一次读都有新发现,有些内容并不是一下子可以弄懂的,每次读似乎都能明白一些概念.再重读一下this关键字.这个概念非常灵活,也非 ...