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的小正方形.对于每个小正方形,地图上已经标注了它的海拔高度以及它是 ...
随机推荐
- python3中zip()函数的用法
>>>a = [1,2,3] >>> b = [4,5,6] >>> c = [4,5,6,7,8] >>> zipped = ...
- CF Round 542 Div1.
B. Wrong Answer 构造一个长度为 2000 的数组,满足最大“子段和 $\times$ 子段长度”比最大子段和刚好大 k sol: 一个比较好的构造方法: 令数组总和为 $S$,然后构造 ...
- 性能测试工具BenchmarkDotnet
.NET Core中的性能测试工具BenchmarkDotnet https://www.cnblogs.com/lwqlun/p/9671611.html 背景介绍 之前一篇博客中,我们讲解.NET ...
- linux大于2T的磁盘格式化
fdisk默认只能格式小于2T的磁盘,我们经常会碰到大于2T的磁盘,我们不能fdisk 格式化. 我们得用parted 来的格式化 parted 命令可能没有,yum install -y parte ...
- vertex shader must minimally write all four components of POSITION
Though the POSITION semantic must be written out by the vertex shader, it cannot be read in by the p ...
- 11.Selenium+Python案例--百度
一.具体代码实现 from selenium import webdriver from selenium.webdriver.common.action_chains import ActionCh ...
- .Net学习资源整理
.Net学习资源整理 ASP.NET Core
- 使用cef
win10的同学注意了按右键以管理员模式启动cmake-gui.exe在Where is the source code:里填上你解压的CEF3路径,如:F:\cef3\cef_binary_3.23 ...
- Linux网络编程——tcp并发服务器(poll实现)
想详细彻底地了解poll或看懂下面的代码请参考<Linux网络编程——I/O复用之poll函数> 代码: #include <string.h> #include <st ...
- Cache-Control头
介绍 Cache-Control头在HTTP中有一定的难度,第一它既可以用于请求头,也可以用于响应头(这里主要将响应缓存).第二,它控制着两个缓存,本地缓存:指客户端本地及其中的缓存(大多指浏览器缓存 ...