Luogu345: [POI2007]POW-The Flood
题意
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的更多相关文章
- 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 ...
- BZOJ1101 & 洛谷3455:[POI2007]ZAP——题解
https://www.luogu.org/problemnew/show/3455#sub http://www.lydsy.com/JudgeOnline/problem.php?id=1101 ...
- LeetCode(50):Pow(x, n)
Medium! 题目描述: 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, ...
- 【区块链Go语言实现】Part 2:工作量证明机制POW
0x00 介绍 在上一篇文章中,我们建立了一个非常简单的数据结构,它是区块链数据库的本质.并且,我们实现了以类似链条关系的方式向其中添加区块的功能:每个区块都会链接到前一区块.然而,我们实现的区块链有 ...
- LeetCode第[50]题(Java):Pow(x, n)
题目:求x的n次幂 难度:Medium 题目内容: Implement pow(x, n), which calculates x raised to the power n (xn). 翻译: 实现 ...
- Java关键字:static
通常,当创建类时,就是在描述那个类的外观和行为.只有用new创建类的对象时,才分配数据存储空间,方法才能被调用.但往往我们会有下面两种需求: 1.我想要这样一个存储空间:不管创建多少对象,无论是不创建 ...
- 浅谈iptables防SYN Flood攻击和CC攻击
------------------------本文为自己实践所总结,概念性的东西不全,这里粗劣提下而已,网上很多,本文主要说下目前较流行的syn洪水攻击和cc攻击------------------ ...
- python之路3:
class set(object): """ set() -> new empty set object set(iterable) -> new set o ...
- 廖雪峰教程笔记:js中map和reduce的用法
举例说明,比如我们有一个函数f(x)=x2,要把这个函数作用在一个数组[1, 2, 3, 4, 5, 6, 7, 8, 9]上,就可以用map实现如下: 由于map()方法定义在JavaScript的 ...
随机推荐
- MySQL创建带有编码的数据库
mysql> create database chao default character set utf8 collate utf8_general_ci;
- is there any way to stop auto block
shadowsocks出现错误日志 tail /var/log/ssserver.log 2017-07-02 12:36:31 ERROR: block all requests from 10.4 ...
- 洛谷 P2762 太空飞行计划问题 P3410 拍照【最大权闭合子图】题解+代码
洛谷 P2762 太空飞行计划问题 P3410 拍照[最大权闭合子图]题解+代码 最大权闭合子图 定义: 如果对于一个点集合,其中任何一个点都不能到达此集合以外的点,这就叫做闭合子图.每个点都有一个权 ...
- 关于 JS 拖拽功能的冲突问题及解决方法
前言 我在之前写过关于 JS 拖拽的文章,实现方式和网上能搜到的方法大致相同,别无二致,但是在一次偶然的测试中发现,这种绑定事件的方式可能会和其它的拖拽事件产生冲突,由此产生了对于事件绑定的思考.本文 ...
- shiro笔记-AuthenticatingRealm和AuthorizingRealm关系
AuthenticatingRealm-------->用于认证方法的Realm AuthorizingRealm--------->用于授权和认证的realm一般使用这个 Authori ...
- C语言_scanf()和getchar() 使用[粗俗易懂]
原文地址:http://blog.csdn.net/hao5743/article/details/6939661/,以下是我重新整理的以下. 问题描述一:[分析scanf()和getchar()读取 ...
- QWebSocket 客户端
QWebSocket 客户端 Public Function - QWebSocket(const QString &origin = QString(),QWebSocketProtocol ...
- 图论4——探索网络流的足迹:Dinic算法
1. 网络流:定义与简析 1.1 网络流是什么? 网络流是一种"类比水流的解决问题方法,与线性规划密切相关"(语出百度百科). 其实,在信息学竞赛中,简单的网络流并不需要太高深的数 ...
- ActiveMq笔记3-AMQ高可用性理论
单点的ActiveMQ作为企业应用无法满足高可用和集群的需求,所以ActiveMQ提供了master-slave.broker cluster等多种部署方式,但通过分析多种部署方式之后我认为需要将两种 ...
- Linux中select poll和epoll的区别
在Linux Socket服务器短编程时,为了处理大量客户的连接请求,需要使用非阻塞I/O和复用,select.poll和epoll是Linux API提供的I/O复用方式,自从Linux 2.6中加 ...