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. 开始使用Apache弗林克和Mapr Streams

    Introduction MapR Ecosystem Package 2.0 (MEP) is coming with some new features related to MapR Strea ...

  2. Hdu 1150

    Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. Laravel5.5/6 报错call to undefined function openssl cipher iv length()

    在安装laravel5.5后, 访问显示报错. call to undefined function openssl cipher iv length() 经查为php7.1的OpenSSL扩展加载失 ...

  4. 从0开始学习 GitHub 系列之「06.团队合作利器 Branch」

    Git 相比于 SVN 最强大的一个地方就在于「分支」,Git 的分支操作简直不要太方便,而实际项目开发中团队合作最依赖的莫过于分支了,关于分支前面的系列也提到过,但是本篇会详细讲述什么是分支.分支的 ...

  5. 记CRenderTarget:DrawText()绘制中文乱码的BUG及解决办法

    原文:记CRenderTarget:DrawText()绘制中文乱码的BUG及解决办法 转载请注明出处:http://www.cnblogs.com/Ray1024 一.问题描述 在MFC中使用Dir ...

  6. linux类库之log4j-LogBack-slf4j-commons-logging

    log4j http://commons.apache.org/proper/commons-logging/ http://logging.apache.org/log4j/2.x/ The Com ...

  7. laravel 图片

    /** * 缩略图上传 */ public static function addPic() { $inputData = request()->all(); $rules = [ 'main_ ...

  8. 【JZOJ4745】【NOIP2016提高A组模拟9.3】看电影

    题目描述 听说NOIP2016大家都考得不错,于是CCF奖励省常中了 K 张变形金刚5的电影票奖励OI队的同学去看电影.可是省常中OI队的同学们共有 N(N >= K)人.于是机智的你想到了一个 ...

  9. 转:国内从事CV相关的企业

    http://blog.csdn.net/carson2005/article/details/7356225 经常碰到朋友问我国内从事计算机视觉(CV)领域的公司的发展情况,产品情况,甚至找工作等问 ...

  10. Directx11教程(19) 画一个简单的地形

    原文:Directx11教程(19) 画一个简单的地形       通常我们在xz平面定义一个二维的网格,然后y的值根据一定的函数计算得到,比如正弦.余弦函数的组合等等,可以得到一个看似不错的地形或者 ...