感觉自己什么题都写不动了。

又是一个神贪心:把所有城市中的点按照高度从小到大排序之后拿出来逐个计算,枚举其他高度小于它的点向四周扩展,如果这个点不能被之前放过的抽水机覆盖,那么把答案加一,并在这个点放上一台抽水机。这个过程适合用并查集来维护。

非常懒的我把地图周围一圈都赋值为无限大。

这样子保证了城市中的所有点能够被最小代价地覆盖。

时间复杂度$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的更多相关文章

  1. [Luogu P3455] [POI2007]ZAP-Queries (莫比乌斯反演 )

    题面 传送门:洛咕 Solution 这题比这题不懂简单到哪里去了 好吧,我们来颓柿子. 为了防止重名,以下所有柿子中的\(x\)既是题目中的\(d\) 为了方便讨论,以下柿子均假设\(b>=a ...

  2. [洛谷3457][POI2007]POW-The Flood

    洛谷题目链接:[POI2007]POW-The Flood 题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方 ...

  3. Luogu P3455 [POI2007]ZAP-Queries

    由于之前做了Luogu P2257 YY的GCD,这里的做法就十分套路了. 建议先看上面一题的推导,这里的话就略去一些共性的地方了. 还是和之前一样设: \[f(d)=\sum_{i=1}^a \su ...

  4. Luogu P3459 [POI2007]MEG-Megalopolis(线段树)

    P3459 [POI2007]MEG-Megalopolis 题意 题目描述 Byteotia has been eventually touched by globalisation, and so ...

  5. Luogu P3462 [POI2007]ODW-Weights

    题目描述 While moving to a new compound the Byteotian Institute of Experimental Physics has encountered ...

  6. 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 ...

  7. Luogu P2522 [HAOI2011]Problem b

    如果你做过[Luogu P3455 POI2007]ZAP-Queries就很好办了,我们发现那一题求的是\(\sum_{i=1}^a\sum_{j=1}^b[\gcd(i,j)=d]\),就是这道题 ...

  8. NOIp2018停课刷题记录

    Preface 老叶说了高中停课但是初中不停的消息后我就为争取民主献出一份力量 其实就是和老师申请了下让我们HW的三个人听课结果真停了 那么还是珍惜这次机会好好提升下自己吧不然就\(AFO\)了 Li ...

  9. 洛谷P3457 [POI2007]POW-The Flood [并查集,模拟]

    题目传送门 pow 题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方形,地图上已经标注了它的海拔高度以及它是 ...

随机推荐

  1. BEC listen and translation exercise 7

    Maybe I ought to subscribe to the Engineering Quarterly.也许我应该订阅<工程学季刊>. The performance is sai ...

  2. Java 代码复用 —— 泛型

    public interface Comparable<T> { public int compareTo(T o); } 1. 接口(Comparable:可比较接口) public s ...

  3. Uncaught TypeError: this.canvas.getContext is not a function

    /**************************************************************************** * Uncaught TypeError: ...

  4. [独孤九剑]Oracle知识点梳理(三)导入、导出

    本系列链接导航: [独孤九剑]Oracle知识点梳理(一)表空间.用户 [独孤九剑]Oracle知识点梳理(二)数据库的连接 [独孤九剑]Oracle知识点梳理(三)导入.导出 [独孤九剑]Oracl ...

  5. How To Uninstall Software Using The Ubuntu Command Line

    How To Uninstall Software Using The Ubuntu Command Line Uninstall Ubuntu Software Using The Terminal ...

  6. Python 设计一个简单的计算器

    设计目标 实现加减乘除及拓号优先级解析 用户输入'1 - 2 * ( (6-3 +(-5/5)*(9-2*3/3 + 7/3*7/4*12 +10 * 5/5 )) - (-4*3)/ (12-3*2 ...

  7. hihoCoder#1068(RMQ-ST算法)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在美国旅行了相当长的一段时间之后,终于准备要回国啦!而在回国之前,他们准备去超市采购一些当地特产——比如汉堡 ...

  8. EMIPLIB简介

    EMIPLIB(http://research.edm.uhasselt.be/emiplib)的全称是'EDM Media over IP libray' .EDM是Hasselt Universi ...

  9. mysql 异常宕机 ..InnoDB: Database page corruption on disk or a failed,,InnoDB: file read of page 8.

    mysql 测试环境异常宕机 系统:\nKylin 3.3 mysql版本:5.6.15--yum安装,麒麟提供的yum源数据库版本 error日志 181218 09:38:52 mysqld_sa ...

  10. Ubuntu bash不记录history方法

    很多都是用: unset HISTORY HISTFILE HISTSAVE HISTZONE HISTORY HISTLOG export HISTFILE=/dev/null export HIS ...