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

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

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

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

时间复杂度$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. python编程实例-dmidecode系统信息搜集

    #!/usr/bin/env python from subprocess import PIPE,Popen def getDmi(): p = Popen(['dmidecode'],stdout ...

  2. uva1636 - Headshot(条件概率)

    简单的条件概率题,直接再来一枪没子弹的概率是所有子串”00“的数目除以‘0’的数目,随机转一下再打没子弹的概率是‘0’的数目除以总数目. #include<iostream> #inclu ...

  3. Java VM(虚拟机) 参数

    -XX:PermSize/-XX:MaxPermSize,永久代内存: 1. 虚拟机参数:-ea,支持 assert 断言关键字 eclipse 默认是不开启此参数的,也就是虽然编译器支持 asser ...

  4. Jenkins之构建触发器配置(转载)

    构建触发器配置,当你在文本框中输入配置的时间后,文本框下方会有时间解释,这样可以很好的看到自己配置的时间对不对. 可以清晰看到我的配置第一个运行时间是周五上午10点执行,第二次是星期六上午10点.   ...

  5. Java 参数的和

    public class CommandParamter { public static void main(String[] args) { // TODO Auto-generated metho ...

  6. controller返回js中文变成?解决方案

    在使用spring-mvc的mvc的时候既享受它带来的便捷,又头痛它的一些问题,比如经典的中文乱码问题.现在是用json作为客户端和服务端 的数据交换格式貌似很流行,但是在springmvc中有时候会 ...

  7. UIAlertController UIAlertView用法

    项目中很多地方会出现弹出框框,来做个判断 基本方法如下 UIAlertController *alertC = [UIAlertController alertControllerWithTitle: ...

  8. Spring 与 @Resource注解

    Spring 中支持@Autowired注解,能够实现bean的注入.同时,Spring 也支持@Resource注解,它和@Autowired类似,都是实现bean的注入.该注解存在javax.an ...

  9. Windows Server 2008 R2换SID要注意

    今天刚装Windows2008R2,准备做实验.同样,我对虚拟机采用了母盘和差异磁盘.在新建好的虚拟机上使用NewSID执行更新SID操作时,一切正常,但当更新完并重启进入系统后,竟然蓝屏了.   原 ...

  10. mysql 替换语句

    将cdb_pms表subject字段中的Welcom to替换成 欢迎光临 UPDATE `cdb_pms` SET `subject` = REPLACE(`subject`, 'Welcome t ...