Codeforces 651E Table Compression【并查集】
来源:互联网 发布:对数据进行加工与处理 编辑:程序博客网 时间:2024/05/16 15:31
题目链接:
http://codeforces.com/problemset/problem/650/C
题意:
给定n*m的矩阵,要求用最小的数表示每个元素,其中各行各列的大小关系保持不变。
分析:
将所有元素从小到大排序,然后找到每个元素相应位置进行填充,由于题目要求是每行每列的大小关系保持不变,所以填充的元素应为所在行和所在列中最大元素+1,保存好各行各列当前的最大值,并根据最后填充的元素不断更新就好啦。
问题是如何处理相同元素以及他们所在行列的更新。
这里使用并查集,将相同元素值的位置保存在一个并查集中,这样就可以保证相同元素在新的矩阵中仍然相等~~~使用并查集新姿势get
代码:
#include<iostream>#include<cstdio>#include<map>#include<algorithm>#include<stack>using namespace std;const int maxn = 1000005;int pa[maxn], _rank[maxn];int v[maxn], row[maxn], cal[maxn], nv[maxn];int m, n;typedef pair<int, int>pii;pii p[maxn];#define fi first#define se secondvoid init(){ for(int i = 0; i < n * m; i++) pa[i] = i;}int _find(int x){ if(pa[x]==x) return x; else return pa[x] = _find(pa[x]);}void unite(int x, int y){ int rx = _find(x), ry = _find(y); if(rx == ry) return; if(_rank[rx]>_rank[ry]) pa[ry]=rx; else { pa[rx] = ry; if(_rank[rx]==_rank[ry]) _rank[ry]++; } return;}bool same(int x, int y){ return _find(x)==_find(y);}int main (void){ scanf("%d%d", &n, &m); for(int i = 0; i < n * m; i++){ scanf("%d",&v[i]); p[i] = pii(v[i], i); } init(); for(int i = 0; i < n; i++){ map<int, int>tmp; for(int j = 0; j < m; j++){ if(tmp.count(v[i * m + j])){ unite(tmp[v[i * m + j]], i * m + j); }else tmp[v[i * m + j]] = i * m + j; } } for(int j = 0; j < m; j++){ map<int, int>tmp; for(int i = 0; i < n; i++){ if(tmp.count(v[i * m + j])){ unite(tmp[v[i * m + j]], i * m + j); }else tmp[v[i * m + j]] = i * m + j; } } sort(p, p + n * m); stack<int>tmp; for(int i = 0; i < n * m; i++){ int id = p[i].se; int x = id / m, y = id % m; nv[_find(id)] = max(nv[_find(id)], max(row[x], cal[y]) + 1); tmp.push(id); if(p[i].fi !=p[i + 1].fi){ while(!tmp.empty()){ id =tmp.top(); tmp.pop(); x = id / m, y = id % m; row[x] = nv[_find(id)]; cal[y] = nv[_find(id)]; } } } for(int i = 0; i < n * m; i++) printf("%d%c", nv[_find(i)], (i + 1)%m == 0?'\n':' '); return 0;}
好吧,我真是弱得cry,想了好久。。。
0 0
- Codeforces 651E Table Compression【并查集】
- Codeforces Round #345 (Div. 2) E. Table Compression(并查集)★ ★
- Codeforces 651E:Table Compression
- Codeforces #345 div1 C. Table Compression 并查集 构造
- Codeforces 650C Table Compression (并查集+拓扑排序)
- Codeforces 651E Table Compression 拓扑序
- Codeforces Round #345 (Div. 1) C. Table Compression (并查集)
- 【Codeforces650C】Table Compression【并查集】【拓扑排序】
- Code Forces 650 C Table Compression(并查集)
- Codeforces Round #345 (Div. 2) E. Table Compression
- Codeforces 9E Interesting Graph and Apples(并查集)
- codeforces 452E Three strings 后缀数组+并查集
- 【CodeForces】466E Information Graph 离线处理+并查集
- Codeforces 466E Information Graph(dfs+并查集)
- Codeforces 452E Three strings 后缀数组 + 并查集
- codeforces #576E Painting Edges 分治+并查集
- Codeforces Round 650E Clockwork Bomb(并查集)
- Codeforces 659E 找环+(并查集/dfs)
- [LeetCode]75. Sort Colors
- [BZOJ 1143][CTSC2008]祭祀river
- 在64位ubuntu上安装tensorflow 以及源码安装
- 如何删除一张表的重复记录
- android的照相图片压缩
- Codeforces 651E Table Compression【并查集】
- iOS开发中 NSRunloop 与NSTimer的问题
- Kalman滤波器
- QML中计时器的使用,实现一个简单的倒计时程序
- SSL 2496 线性递推式 矩阵乘法
- Eclipse注释快捷键
- mysql数据库千万级别数据的查询优化和分页测试
- UVa 1363 约瑟夫的数论问题
- js获取cookie值