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 ...
随机推荐
- vue+ElementUI项目中,上传控件为必填项,上传图片后清空提示信息
(ps:以下是我在项目中遇到得问题及解决方法,希望对你们有帮助.如果还有其他方法,可以留言,谢谢) 一个表单页面,使用element-ui中el-upload上传图片,此项为必填项,然后写了校验规则, ...
- h5滚动页面固定导航
1.需要效果 2.实现方法 (1)原生js实现 document.addEventListener('scroll', function (event) { var scrollDamo = wind ...
- Django项目:CRM(客户关系管理系统)--08--03PerfectCRM创建基本数据03
如果感觉本章博客对您有帮助,请尽情打赏吧!
- bzoj 1024 [SCOI2009]生日快乐——模拟
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1024 可以枚举这边放多少块.那边放多少块. 注意精度.不要每次用x*y/base算有多少块, ...
- QT生成GUID
#include <QCoreApplication> #include <QUuid> #include <QDebug> int main(int argc, ...
- Win7下IIS的安装配置与文件发布
出于兴趣,抽空弄了一下Windows上的IIS,把过程记录下来与大家分享.高手请略过... 一.安装IIS.打开控制面板,单击“程序与功能” 二.点击左侧“打开或关闭Windows功能” 三.找到“I ...
- 第二周<线性回归>
可行性分析 略 sklearn.linear_model.linear_regression() 一些参数 fit_intercept 布尔型参数,表示是否计算该模型的截距 normalize 布尔型 ...
- 在Liferay 7中如何自定义一个Portlet的toolbar
哈哈,懒得自己写了,直接贴教程了,你想为那个portlet添加自定义的toolbar,就在javax.portlet.name=属性中写上它的值.教程博客:Adding Portlet URL in ...
- Mathematica 和 MATLAB、Maple 并称为三大数学软件
Mathematica是一款科学计算软件,很好地结合了数值和符号计算引擎.图形系统.编程语言.文本系统.和与其他应用程序的高级连接.很多功能在相应领域内处于世界领先地位,它也是使用最广泛的数学软件之一 ...
- day39-Spring 13-Spring的JDBC模板:默认连接池的配置
Spring内置的连接池DriverManagerDataSource的源码. /* * Copyright 2002-2008 the original author or authors. * * ...