题意

见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. Sql server 卸载方法

    sql server不正确卸载时,重新安装会失败,会提示各种错误:如数据库实例已存在等... 下面是我摸索总结出来的卸载方法,以及重装失败后的处理方法: 卸载方法: 注意:SQL Server 200 ...

  2. .Net Core和jexus配置HTTPS服务

    花了几天时间,看了好多篇博客,终于搞定了网站的HTTPS服务,借此写篇博客,来让有需要的朋友少走弯路. 一.环境介绍 1.Linux下在Docker容器中部署好了一个网站,该网站需要通过外部提供程序访 ...

  3. intellij idea maven springmvc 环境搭建

    1.   新建maven 工程 intellij idea 默认已经集成了maven, 直接点击下一步 2.   配置文件修改 pom.xml 文件 <?xml version="1. ...

  4. HBuilder常用快捷键

    切换tab: Ctrl+Tab全部保存: Ctrl+Shift+S 激活代码助手: Alt+/显示方法参数提示: Alt+Shift+?转到定义: Ctrl+Alt+D 开启关闭注释整行: Ctrl+ ...

  5. Spring 中出现Element : property Bean definitions can have zero or more properties. Property elements correspond to JavaBean setter methods exposed by the bean classes. Spring supports primitives, refer

    在这个ApplicationContext.xml文件中出现 如下报错 Element : property Bean definitions can have zero or more proper ...

  6. CentOS命令修改系统时间同步

    使用Centos,遇到本地时间对不上,直接敲命令:date -s "2016-01-08  15:15:15"是立即生效了,但是重启后,系统时间还是原来的. 修改了其一是没有办法奏 ...

  7. Centos下的GitLab的安装汉化和数据备份以及管理员密码重置

    前言: 安装版本:gitlab-ce-8.8.5-ce.1.el7.x86_64.rpm 下载地址: https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yu ...

  8. PAT乙级1065 map

    思路:检查某个客人是否有伴侣,如果有,伴侣是否也出现即可. 注意:0个单身狗的时候,不要输出多余的'\n', 否则会出现格式错误. AC代码 #include <stdio.h> #inc ...

  9. vector动态数组

    vector是STL模板库中的序列式容器,利用它可以有效地避免空间的浪费. 创建vector容器 vector< int >v:vector< char >:vector< ...

  10. uva1471 二叉搜索树

    此题紫书上面有详细分析,关键是运用Set优化实现O(nlgn)复杂度 AC代码: #include<cstdio> #include<set> #include<algo ...