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)的更多相关文章

  1. 图像处理之泛洪填充算法(Flood Fill Algorithm)

    泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...

  2. 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能

    泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...

  3. [LeetCode] Flood Fill 洪水填充

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

  4. [Swift]LeetCode733. 图像渲染 | Flood Fill

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

  5. LeetCode刷题 Flood Fill 洪水填充问题

    An  image is represented by a 2-D array of integers,each integers,each integer respresenting the sta ...

  6. [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 ...

  7. LeetCode - Flood Fill

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

  8. CF 1114 D. Flood Fill

    D. Flood Fill 链接 题意: 一个颜色序列,每个位置有一个颜色,选择一个起始位置,每次可以改变包含这个位置的颜色段,将这个颜色段修改为任意一个颜色, 问最少操作多少次.n<=5000 ...

  9. 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 ...

随机推荐

  1. 请自行检查是否安装VC9运行库??

    phpStudy是一款PHP调试环境的程序集成包,该程序包集成最新的Apache+PHP+MySQL+phpMyAdmin+ZendOptimizer,一次性安装,无须配置即可使用,是非常方便.好用的 ...

  2. eclipse修改中文注释的字体(亲测有用!)

    Window –> Preferences –> General –> Appearance –> Colors and Fonts –> Basic –> Tex ...

  3. Python数据分析与展示[第二周]

    matplotlib 有各种可视化的类构成 一般调用 matplotlib.pypolt 这个命令字库 相当于快捷方式 plt.plot(a) 只有一个一维列表 x轴充当列表索引 plt.ylabel ...

  4. js的动态tab导航

    html部分 <div class="container"> <h3 class="page-header">tab切换</h3& ...

  5. Python接口自动化(一)接口基础

    HTTP接口熟悉 常见接口介绍 接口工具的使用 fiddler如何mock数据 常见接口基础面试 如何理解接口?前后端解耦,前端和后端数据对接桥梁 接口测试和功能测试区别在哪?接口测试是功能测试的一种 ...

  6. linux下播放器设计和开发

    http://blog.csdn.net/henryjee/article/details/6737392 本文根据DawnLightPlayer的开发经验写成.DawnLithtPlayer是今天3 ...

  7. day16 web前端之JavaScript

    页面布局补充 样例页面: 示例代码: <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  8. Mongodb停止和启动

    mongodb开启.停止.重启操作 #开启service mongodb start#停止service mongodb stop#重启service mongodb restart

  9. 外贸电子商务网站之Prestashop paypal支付添加

    1.在https://addons.prestashop.com/en/payment-card-wallet/1748-paypal.html 下载paypal支付模块 2.解压,复制到网站根目录- ...

  10. 微服务开源生态报告 No.4

    「微服务开源生态报告」,汇集各个开源项目近期的社区动态,帮助开发者们更高效的了解到各开源项目的最新进展. 社区动态包括,但不限于:版本发布.人员动态.项目动态和规划.培训和活动. 非常欢迎国内其他微服 ...