题意

见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. shell 脚本中执行SQL语句 -e "..."

    /usr/local/mysql/bin/mysql -uroot -p123456 -e " use faygo source faygo.sql select * from devqui ...

  2. css scale 元素放大缩小效果

    <style> .trans-scale { width: 300px; height:300px; margin:100px auto; background:#99F; transit ...

  3. selenium自动化测试——常见的八种元素定位方法

    selenium常用的八种元素定位方法 1.通过 id 定位:find_element_by_id() 2.通过 name 定位:find_element_by_name() 3.通过 tag 定位: ...

  4. js中的各种“位置”——“top、clientTop、scrollTop、offsetTop……”,你知道多少

    当要做一些与位置相关的插件或效果的时候,像top.clientTop.scrollTop.offsetTop.scrollHeight.clientHeight.offsetParent...看到这么 ...

  5. Maven中的pom.xml详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. FFMpeg for PHP

    PHP使用FFMpeg来转换视频格式.Github上搜索FFMPEG,到https://github.com/PHP-FFMpeg/PHP-FFMpeg. For Windows users : Pl ...

  7. MySQL的BlackHole引擎在主从架构中的作用

    MySQL在5.x系列提供了Blackhole引擎–“黑洞”. 其作用正如其名字一样:任何写入到此引擎的数据均会被丢弃掉, 不做实际存储:Select语句的内容永远是空. 和Linux中的 /dev/ ...

  8. Duilib第一步(III)-知识进阶

    核心模块 CWindowWnd:窗口对象管理父类 创建窗口. 窗口消息过程处理. 提供窗口子类化.超类化接口. CDialogBuilder:空间布局类 解析XML界面布局文件,构建控件树 创建控件对 ...

  9. 使用Socket对序列化数据进行传输(基于C#)

    客户端代码 [Serializable] // 表示该类可以被序列化 class Person{ public string name; public void HI() { Debug.Log(na ...

  10. Qt 如何使用 lambda 表达式连接信号和槽?

    connect(camera, static_cast<void(QCamera::*)(QCamera::LockStatus, QCamera::LockChangeReason)>( ...