题意

见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. block,inline,inline-block的区别

    最近正在复习,紧张地准备几天后的笔试,然后刚好看到这个地方. block:块级元素,会换行,如div,p,h1~h6,table这些,可以设置宽高:    inline:行内元素,不换行,挤在一行显示 ...

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

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

  3. LNMP搭建04 -- 配置Nginx支持PHP

    首先建立存放网页文件的目录,执行 mkdri /usr/local/server/www  然后进入到该目录中 cd /usr/local/server/www 然后创建一个测试文件: phpinfo ...

  4. cocos2d-x中处理touch事件

    在cocos2d-x中, touch事件分为两种:一种是单点事件, 另一种是多点事件. 单点事件对应的代理方法是: virtual bool ccTouchBegan(CCTouch *pTouch, ...

  5. Java经典编程题50道之十五

    输入三个整数x,y,z,请把这三个数由小到大输出. public class Example15 {    public static void main(String[] args) {       ...

  6. 一个简单的定时表达式(HH:mm:ss)解析

    前言 为客户开发了一个日志监控程序,监听各频道是否正常工作.其中有一个功能是这样的,当所有频道正常运行一段时间后,语音提示值班人员系统运行正常.一开始,想法比较简单,设置了一个变量,在线程不断轮询的过 ...

  7. DxPackNet 3.音频捕捉(录音)

    用DxpackNet捕捉音频其实很简单 1.初始化控件 IDxMicrophCapture microphone; private void Form1_Load(object sender, Eve ...

  8. c++ 如何把RGB图像转换成HSV图像?

    CV_<bit_depth>(S|U|F)C<number_of_channels> 1--bit_depth---比特数---代表8bite,16bites,32bites, ...

  9. 《设计模式之禅》--设计模式大PK

    创建类模式包括工厂方法模式.建造者模式.抽象工厂模式.单例模式和原型模式. 其中单例模式要保持在内存中只有一个对象,原型模式是要求通过复制的方式产生一个新的对象. [工厂方法(抽象工厂) VS 建造者 ...

  10. CodeForces - 796B 模拟

    思路:模拟移动即可,如果球落入洞中停止移动.注意:有可能第一个位置就是洞!! AC代码 #include <cstdio> #include <cmath> #include ...