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 ...
随机推荐
- golang学习资料必备
核心资料库 https://github.com/yangwenmai/learning-golang
- windows下maven的安装配置
什么是maven Maven是基于POM(工程对象模型),通过一小段描述来对项目的代码.报告.文件进管理的工具. Maven是一个跨平台的项目管理工具,它是使用java开发的,它要依赖于jdk1.6及 ...
- sublime3安装javascript控制台环境 方法1
Sublime Text 3 http://www.sublimetext.com/3 node.js http://nodejs.cn/download 1.安装完成后. 2.打 ...
- C++学习笔记(1)-构造函数与析构函数
1.C++规定,每个类必须有默认的构造函数,没有构造函数就不能创建对象. 2.若没有提供任何构造函数,那么c++自动提供一个默认的构造函数,该默认构造函数是一个没有参数的构造函数,它仅仅负责创建对象而 ...
- Idea下面无法识别web-inf下lib目录的子目录的jar包,只能直接放lib下面才能识别?
解决方案一: Ctrl+Alt+Shift+s打开projuect Structure-->Livraries-->➕-->java-->选择对应的lib目录即可! 解决方案二 ...
- pl/sql 语句块循环语句
---基本循环declarev1 number(2) :=1;begin loop dbms_output.put_line(v1); v1:=v1+1; exit when v1>10; -- ...
- Gradle基本操作入手
Gradle本身的领域对象主要由Project和Task.Project为Task提供了执行上下文,所有的Plugin要么向Project中添加用于配置Property,要么向Project中添加不同 ...
- Bnd教程(1):如何用命令行安装bnd
1. 如果你用的是MacOS,请运行: brew install bnd 然后运行bnd version看是否安装成功: $ bnd version 4.0.0.201801191620-SNAPSH ...
- LintCode_67 二叉树中序遍历
题目 给出一棵二叉树,返回其中序遍历 C++ 非递归 vector<int> inorderTraversal(TreeNode *root) { // write your code h ...
- 常用命令(java、linux)
1.java打war包: jar cvf temp.war */ . 命令格式: java cvf 打包文件名称 要打包的目录 打包文件保存路径 解压: jar xvf temp.war ...