题意

见luogu

Sol

贪心

从小到大枚举高度,把小于等于这一高度的相邻格子用并查集合并

那么这个集合内的所有格子都一定可以由这个集合内的一个最低点抽完水

那么合并之后(一定要在合并之后)

判断这一高度是否有城市,有则检查它所在的集合是否放了抽水机,没有就在这个集合中放一个

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
# define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
using namespace std;
typedef long long ll;
const int _(1005); IL int Input(){
RG int x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
} int n, m, ans, h[_][_], id[_][_], cnt, type[_][_], done[_ * _];
int que[_ * _];
int fa[_ * _], dx[4] = {1, -1}, dy[4] = {0, 0, 1, -1};
struct Data{
int x, y, id; IL bool operator <(RG Data B) const{
return h[x][y] < h[B.x][B.y];
}
} p[_ * _]; IL int Find(RG int x){
return fa[x] == x ? x : fa[x] = Find(fa[x]);
} IL void Union(RG int Sx, RG int Sy, RG int fx){
for(RG int i = 0; i < 4; ++i){
RG int xx = Sx + dx[i], yy = Sy + dy[i];
if(xx < 1 || yy < 1 || xx > n || yy > m || h[xx][yy] > h[Sx][Sy]) continue;
RG int fy = Find(id[xx][yy]); fx = Find(fx);
if(fx == fy) continue;
done[fx] |= done[fy], fa[fy] = fx;
}
} int main(RG int argc, RG char* argv[]){
n = Input(), m = Input();
for(RG int i = 1; i <= n; ++i)
for(RG int j = 1; j <= m; ++j){
h[i][j] = Input(), type[i][j] = h[i][j] > 0;
if(!type[i][j]) h[i][j] = -h[i][j];
id[i][j] = ++cnt, fa[cnt] = cnt, p[cnt] = (Data){i, j, cnt};
}
sort(p + 1, p + cnt + 1);
for(RG int i = 1, now; i <= cnt; ++i){
if(h[p[i].x][p[i].y] != now){
while(que[0]){
RG int x = Find(que[que[0]--]);
if(!done[x]) done[x] = 1, ++ans;
}
now = h[p[i].x][p[i].y];
}
Union(p[i].x, p[i].y, p[i].id);
if(type[p[i].x][p[i].y]) que[++que[0]] = p[i].id;
}
while(que[0]){
RG int x = Find(que[que[0]--]);
if(!done[x]) done[x] = 1, ++ans;
}
printf("%d\n", ans);
return 0;
}

Luogu345: [POI2007]POW-The Flood的更多相关文章

  1. HDU - 6433: H. Pow (简答题,输出大数)

    There are n numbers 3^0, 3^1, . . . , 3^n-1. Each time you can choose a subset of them (may be empty ...

  2. BZOJ1101 & 洛谷3455:[POI2007]ZAP——题解

    https://www.luogu.org/problemnew/show/3455#sub http://www.lydsy.com/JudgeOnline/problem.php?id=1101 ...

  3. LeetCode(50):Pow(x, n)

    Medium! 题目描述: 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, ...

  4. 【区块链Go语言实现】Part 2:工作量证明机制POW

    0x00 介绍 在上一篇文章中,我们建立了一个非常简单的数据结构,它是区块链数据库的本质.并且,我们实现了以类似链条关系的方式向其中添加区块的功能:每个区块都会链接到前一区块.然而,我们实现的区块链有 ...

  5. LeetCode第[50]题(Java):Pow(x, n)

    题目:求x的n次幂 难度:Medium 题目内容: Implement pow(x, n), which calculates x raised to the power n (xn). 翻译: 实现 ...

  6. Java关键字:static

    通常,当创建类时,就是在描述那个类的外观和行为.只有用new创建类的对象时,才分配数据存储空间,方法才能被调用.但往往我们会有下面两种需求: 1.我想要这样一个存储空间:不管创建多少对象,无论是不创建 ...

  7. 浅谈iptables防SYN Flood攻击和CC攻击

    ------------------------本文为自己实践所总结,概念性的东西不全,这里粗劣提下而已,网上很多,本文主要说下目前较流行的syn洪水攻击和cc攻击------------------ ...

  8. python之路3:

    class set(object): """ set() -> new empty set object set(iterable) -> new set o ...

  9. 廖雪峰教程笔记:js中map和reduce的用法

    举例说明,比如我们有一个函数f(x)=x2,要把这个函数作用在一个数组[1, 2, 3, 4, 5, 6, 7, 8, 9]上,就可以用map实现如下: 由于map()方法定义在JavaScript的 ...

随机推荐

  1. [Python Study Notes]CS架构远程访问获取信息--SERVER端v2.0

    更新内容: 1.增加内存信息获取 2.增加电池信息获取 3.增加磁盘信息获取 4.重新布局窗体 5.增加窗体名称 6.增加连接成功之前,不可按压 ''''''''''''''''''''''''''' ...

  2. 企业级docker仓库Harbor部署

    1.安装环境下载离线安装包地址https://github.com/vmware/harbor/releases/yum install -y dockerpip install -i https:/ ...

  3. nginx上支持.htaccess伪静态的配置实例

    本文介绍下,在nginx上配置.htaccess伪静态的方法,有需要的朋友参考下吧. 在apache上.htaccess转向,只要apache编译的时候指明支持rewrite模块即可. 但是换到ngi ...

  4. mac攻略(4) -- 使用brew配置php7开发环境(mac+php+apache+mysql+redis)

    [http://www.cnblogs.com/redirect/p/6131751.html] 网上有很多文章都是错误的,因为是copy别人的,作者没有自己亲测,不仅不能给新手提供帮助,还会产生严重 ...

  5. php获取中文字符拼音首字母

    //php获取中文字符拼音首字母 function getFirstCharter($str){ if(empty($str)){ return ''; } $fchar = ord($str{0}) ...

  6. mysql数据库外部无法访问

    有以下两种情况: 1.mysql未分配访问权限 格式:grant 权限 on 数据库名.表名 用户@登录主机 identified by "用户密码"; grant select, ...

  7. mac中的myeclipse的控制台中文乱码问题解决办法

    之前写java用到控制台的主要是字符和数字,中文输入貌似真的还没用过,所以就遇到了一个悲剧的老问题,估计每个程序员都会遇到——中文乱码. 用的是MyEclipse开发环境,Window->Gen ...

  8. js中checkbox的全选和反选的实现

    <head> <meta charset="utf-8"/> <script type="text/javascript"> ...

  9. Qt Create or VS 2015 使用 Opencv330 相机静态库链接错误如何解决?

    查看链接库,添加 vfw32.lib 即可.

  10. Google 浏览器被劫持怎么办?

    chrome://version/ 输入以上语句,在命令行中可看到恶意网址,复制该命令行,修改后面的网址即可