Luogu 3457 [POI2007]POW-The Flood
感觉自己什么题都写不动了。
又是一个神贪心:把所有城市中的点按照高度从小到大排序之后拿出来逐个计算,枚举其他高度小于它的点向四周扩展,如果这个点不能被之前放过的抽水机覆盖,那么把答案加一,并在这个点放上一台抽水机。这个过程适合用并查集来维护。
非常懒的我把地图周围一圈都赋值为无限大。
这样子保证了城市中的所有点能够被最小代价地覆盖。
时间复杂度$O(nm log nm)$,有点卡常。
Code:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int N = ;
const int dx[] = {, , -, };
const int dy[] = {, , , -};
const int inf = << ; int n, m, tot = , a[N][N], ufs[N * N];
bool vis[N * N]; struct Node {
int x, y, val; friend bool operator < (const Node &u, const Node &v) {
return u.val < v.val;
} } b[N * N], c[N * N]; inline void read(int &X) {
X = ; char ch = ; int op = ;
for(; ch > ''|| ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline int id(int x, int y) {
return (x - ) * m + y;
} int find(int x) {
return ufs[x] == x ? x : ufs[x] = find(ufs[x]);
} inline void merge(int x, int y) {
int fx = find(x), fy = find(y);
if(fx == fy) return;
ufs[fx] = fy;
if(vis[fx]) vis[fy] = ;
} inline void ext(int x, int y) {
for(int i = ; i < ; i++) {
int tox = x + dx[i], toy = y + dy[i];
if(a[tox][toy] <= a[x][y]) merge(id(tox, toy), id(x, y));
}
} int main() {
read(n), read(m);
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++) {
read(a[i][j]);
if(a[i][j] > ) c[++tot] = (Node) {i, j, a[i][j]};
else a[i][j] *= -;
b[id(i, j)].x = i, b[id(i, j)].y = j, b[id(i, j)].val = a[i][j];
ufs[id(i, j)] = id(i, j);
} for(int i = ; i <= m + ; i++)
a[][i] = a[n + ][i] = inf;
for(int i = ; i <= n + ; i++)
a[i][] = a[i][m + ] = inf; sort(b + , b + + n * m);
sort(c + , c + + tot); /* for(int i = 1; i <= tot; i++)
printf("%d %d %d\n", c[i].x, c[i].y, c[i].val); */ int ans = ;
for(int j = , i = ; i <= tot; i++) {
for(; j <= n * m && c[i].val >= b[j].val; j++)
ext(b[j].x, b[j].y);
int now = find(id(c[i].x, c[i].y));
if(!vis[now]) {
vis[now] = ;
ans++;
}
} printf("%d\n", ans);
return ;
}
Luogu 3457 [POI2007]POW-The Flood的更多相关文章
- [Luogu P3455] [POI2007]ZAP-Queries (莫比乌斯反演 )
题面 传送门:洛咕 Solution 这题比这题不懂简单到哪里去了 好吧,我们来颓柿子. 为了防止重名,以下所有柿子中的\(x\)既是题目中的\(d\) 为了方便讨论,以下柿子均假设\(b>=a ...
- [洛谷3457][POI2007]POW-The Flood
洛谷题目链接:[POI2007]POW-The Flood 题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方 ...
- Luogu P3455 [POI2007]ZAP-Queries
由于之前做了Luogu P2257 YY的GCD,这里的做法就十分套路了. 建议先看上面一题的推导,这里的话就略去一些共性的地方了. 还是和之前一样设: \[f(d)=\sum_{i=1}^a \su ...
- Luogu P3459 [POI2007]MEG-Megalopolis(线段树)
P3459 [POI2007]MEG-Megalopolis 题意 题目描述 Byteotia has been eventually touched by globalisation, and so ...
- Luogu P3462 [POI2007]ODW-Weights
题目描述 While moving to a new compound the Byteotian Institute of Experimental Physics has encountered ...
- BZOJ 1107: [POI2007]驾驶考试egz / Luogu P3463 [POI2007]EGZ-Driving Exam (树状数组 LIS)
能从iii走到所有跑道 相当于 能从iii走到111和nnn. 边反向后就相当于 能从111和nnn走到iii. 为了方便叙述,把111~nnn叫做x坐标,111~(m+1)(m+1)(m+1)叫做y ...
- Luogu P2522 [HAOI2011]Problem b
如果你做过[Luogu P3455 POI2007]ZAP-Queries就很好办了,我们发现那一题求的是\(\sum_{i=1}^a\sum_{j=1}^b[\gcd(i,j)=d]\),就是这道题 ...
- NOIp2018停课刷题记录
Preface 老叶说了高中停课但是初中不停的消息后我就为争取民主献出一份力量 其实就是和老师申请了下让我们HW的三个人听课结果真停了 那么还是珍惜这次机会好好提升下自己吧不然就\(AFO\)了 Li ...
- 洛谷P3457 [POI2007]POW-The Flood [并查集,模拟]
题目传送门 pow 题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方形,地图上已经标注了它的海拔高度以及它是 ...
随机推荐
- 转: 全局变量报错:UnboundLocalError: local variable 'l' referenced before assignment
http://blog.csdn.net/my2010sam/article/details/17735159
- vmem驱动设备
vmem是内存多字符设备.包含vfs的open.read.write.ioctl.poll.fasync和release函数,device文件的读写. virtual_mem.c #include & ...
- Oracle 高可用作业测试
1.RAC 是一种: A 分布式数据库B 内存共享多节点数据库C 能够保护数据安全的架构 2.RAC 和CRS 的区别是: A 前者表示一种架构,后者是一套软件B 前者表示一套软件,后者是一种架构 C ...
- 数据库学习笔记 (三) python操作数据库
python 操作MYSQL数据库主要有两种方式: 使用原生模块:pymysql ORM框架:SQLAchemy 一.pymysql 1.1下载安装模块 第一种:cmd下:执行命令下载安装:pip3 ...
- 2014.2.23加载大数据时不闪烁的listView
namespace 相册处理 { //将ListView重载为此新类,解决加载过程中闪烁的问题 //在designer.cs中改写: //private ListViewNeverFlickering ...
- JavaScript基本概念B - 关于方法
方法也是对象 这个事需要反复强调.方法是 类型 Function 的对象,和其他对象一样,它也有方法. function gen() { return function ans(factor) { r ...
- centos7的xfs配置
XFS是扩展性高.高性能的文件系统.也是rhel7/centos7的默认文件系统.XFS支持metadata journaling,这使其能从crash中更快速的恢复.它也支持在挂载和活动的状态下进行 ...
- JavaScript语言基础-包装对象
- ORA-00904: 标识符无效——解决方案
转自:https://blog.csdn.net/jajavaja/article/details/49122639 建表时列名用双引号引着(用Navicat工具建表默认是加上双引号的),java连接 ...
- django之admin组件
一.面向对象复习 1.类的继承 class Base(object): def __init__(self,val): self.val = val def func(self): self.test ...