zoj 3859 DoIt is Being Flooded (MFSet && Flood Fill)
ZOJ :: Problems :: Show Problem
这题开始的时候想不到怎么调整每个grid的实际淹没时间,于是只好找了下watashi的题解,发现这个操作还是挺简单的。
ZOJ3354 | ゆっくりでいいさ debug这题累死了。。解释还是移步看watashi大神的吧。。
除了开始的时候这个调整,后面并查集的部分是相当容易想到的,其实就是用并查集了统计每一个块的个数。
代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue> using namespace std; int n, m;
inline int con(int x, int y) { return x * m + y;}
inline bool inmat(int x, int y) { return <= x && x < n && <= y && y < m;} const int dx[] = { , -, , };
const int dy[] = { -, , , };
const int N = ;
const int M = N * N; struct MFS {
int fa[M], cnt[M];
int fx, fy;
void init() { for (int i = ; i < M; i++) fa[i] = i, cnt[i] = ;}
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]);}
void merge(int x, int y) {
fx = find(x), fy = find(y);
if (fx == fy) return ;
fa[fy] = fx;
cnt[fx] += cnt[fy];
}
bool same(int x, int y) { return find(x) == find(y);}
int count(int x) { return cnt[find(x)];}
} mfs;
bool vis[N][N];
int qry[][], hmk[]; struct Grid {
int h, x, y;
Grid() {}
Grid(int h, int x, int y) : h(h), x(x), y(y) {}
bool operator < (Grid a) const { return h > a.h;}
} grid[M]; int mat[N][N];
map<int, int> id;
map<int, int> cur, rec[];
priority_queue<Grid> pq; int bitcnt(int x) {
int ret = ;
for (int i = ; i < ; i++) if (x & << i) ret++;
return ret;
} int main() {
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
int matsz, t;
int cx, cy, nx, ny;
while (~scanf("%d%d", &n, &m)) {
matsz = n * m, t = ;
while (!pq.empty()) pq.pop();
memset(vis, , sizeof(vis));
for (int i = ; i < n; i++) for (int j = ; j < m; j++) {
scanf("%d", &mat[i][j]);
if (i == || j == || i == n - || j == m - ) {
grid[t] = Grid(mat[i][j], i, j);
pq.push(grid[t]);
t++;
vis[i][j] = true;
}
}
while (!pq.empty()) {
cx = pq.top().x, cy = pq.top().y;
pq.pop();
for (int i = ; i < ; i++) {
nx = cx + dx[i];
ny = cy + dy[i];
if (inmat(nx, ny)) {
if (vis[nx][ny]) continue;
mat[nx][ny] = max(mat[nx][ny], mat[cx][cy]);
grid[t] = Grid(mat[nx][ny], nx, ny);
pq.push(grid[t]);
t++;
vis[nx][ny] = true;
}
}
}
sort(grid, grid + matsz);
int k;
scanf("%d", &k);
for (int i = ; i < k; i++) {
for (int j = ; j < ; j++) scanf("%d", &qry[i][j]);
hmk[i] = qry[i][];
}
sort(hmk, hmk + k);
int sz = unique(hmk, hmk + k) - hmk;
reverse(hmk, hmk + sz);
id.clear();
cur.clear();
memset(vis, , sizeof(vis));
mfs.init();
for (int i = , c = , a, b; i < sz; i++) {
id[hmk[i]] = i;
while (c < matsz && grid[c].h > hmk[i]) {
cx = grid[c].x, cy = grid[c].y;
vis[cx][cy] = true;
cur[]++;
for (int d = ; d < ; d++) {
nx = cx + dx[d], ny = cy + dy[d];
if (!inmat(nx, ny) || !vis[nx][ny]) continue;
a = con(cx, cy), b = con(nx, ny);
if (!mfs.same(a, b)) {
int ca = mfs.count(a);
int cb = mfs.count(b);
cur[ca]--;
if (cur[ca] == ) cur.erase(ca);
cur[cb]--;
if (cur[cb] == ) cur.erase(cb);
mfs.merge(a, b);
cur[mfs.count(a)]++;
}
}
c++;
}
rec[i] = cur;
}
map<int, int>::iterator mii;
int ans, tid;
for (int i = ; i < k; i++) {
ans = , tid = id[qry[i][]];
for (mii = rec[tid].begin(); mii != rec[tid].end(); mii++) {
ans += (*mii).second << bitcnt((*mii).first & qry[i][]);
}
printf("%d\n", ans);
}
}
return ;
}
zoj 3859 DoIt is Being Flooded (MFSet && Flood Fill)的更多相关文章
- 图像处理之泛洪填充算法(Flood Fill Algorithm)
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
- 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能
泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...
- [LeetCode] Flood Fill 洪水填充
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- [Swift]LeetCode733. 图像渲染 | Flood Fill
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- LeetCode刷题 Flood Fill 洪水填充问题
An image is represented by a 2-D array of integers,each integers,each integer respresenting the sta ...
- [LeetCode&Python] Problem 733. Flood Fill
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- LeetCode - Flood Fill
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- CF 1114 D. Flood Fill
D. Flood Fill 链接 题意: 一个颜色序列,每个位置有一个颜色,选择一个起始位置,每次可以改变包含这个位置的颜色段,将这个颜色段修改为任意一个颜色, 问最少操作多少次.n<=5000 ...
- Codeforces Round #538 (Div. 2) D. Flood Fill 【区间dp || LPS (最长回文序列)】
任意门:http://codeforces.com/contest/1114/problem/D D. Flood Fill time limit per test 2 seconds memory ...
随机推荐
- oracle习题集-高级查询2
1.列出员工表中每个部门的员工数和部门编号 Select deptno,count(*) from emp group by deptno; 2.列出员工表中,员工人数大于3的部门编号和员工人数 ; ...
- 2019-9-2-win10-uwp-存放网络图片到本地
title author date CreateTime categories win10 uwp 存放网络图片到本地 lindexi 2019-09-02 12:57:38 +0800 2018-2 ...
- C++多态小结
C++ 多态 多态 多态按字面的意思就是多种形态.当类之间存在层次结构,并且类之间是通过继承关联时,就会用到多态. C++ 多态意味着调用成员函数时,会根据调用函数的对象的类型来执行不同的函数. 多态 ...
- Vue--moment时间格式插件安装和使用
使用链接:http://momentjs.cn/ 1.安装monent 2.导入 3.过滤器 4.template模板使用:
- 【笔记】http协议笔记
本文是本人在复习http协议时,手动整理的资料,以备后续查阅. http(hypertext transfer protocol):超文本协议.是万维网(world wide web,www,也简称为 ...
- Linux平台的SVN服务器的配置及搭建
https://jingyan.baidu.com/article/54b6b9c08b35382d593b477c.html 一.安装SVN 1 在Linux平台上,SVN的软件包名称是subv ...
- awk notes
字符串拼接 cat sql | awk '{print " ALTER TABLE tableA ALTER " $1 " TYPE " $2 " ...
- POJ 1150 The Last Non-zero Digit 数论+容斥
POJ 1150 The Last Non-zero Digit 数论+容斥 题目地址: id=1150" rel="nofollow" style="colo ...
- DirectX11笔记(八)--Direct3D渲染4--VERTEX SHADER
原文:DirectX11笔记(八)--Direct3D渲染4--VERTEX SHADER 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u0103 ...
- 记CRenderTarget:DrawText()绘制中文乱码的BUG及解决办法
原文:记CRenderTarget:DrawText()绘制中文乱码的BUG及解决办法 转载请注明出处:http://www.cnblogs.com/Ray1024 一.问题描述 在MFC中使用Dir ...