Codeforces Round #345 (Div. 2) E. Table Compression 并查集
E. Table Compression
题目连接:
http://www.codeforces.com/contest/651/problem/E
Description
Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many others. Inspired by the new knowledge, Petya is now developing the new compression algorithm which he wants to name dis.
Petya decided to compress tables. He is given a table a consisting of n rows and m columns that is filled with positive integers. He wants to build the table a' consisting of positive integers such that the relative order of the elements in each row and each column remains the same. That is, if in some row i of the initial table ai, j < ai, k, then in the resulting table a'i, j < a'i, k, and if ai, j = ai, k then a'i, j = a'i, k. Similarly, if in some column j of the initial table ai, j < ap, j then in compressed table a'i, j < a'p, j and if ai, j = ap, j then a'i, j = a'p, j.
Because large values require more space to store them, the maximum value in a' should be as small as possible.
Petya is good in theory, however, he needs your help to implement the algorithm.
Input
The first line of the input contains two integers n and m (, the number of rows and the number of columns of the table respectively.
Each of the following n rows contain m integers ai, j (1 ≤ ai, j ≤ 109) that are the values in the table.
Output
Output the compressed table in form of n lines each containing m integers.
If there exist several answers such that the maximum number in the compressed table is minimum possible, you are allowed to output any of them.
Sample Input
2 2
1 2
3 4
Sample Output
1 2
2 3
Hint
题意
给一个n*m的矩阵,然后让你压缩一下,输出另外一个n*m的矩阵。
这两个矩阵要求在每一行每一列的大小关系保持不变。
比如ai,j<ai,k那么第二个矩阵也得满足这个条件。
题解:
从小到大排序,显然最小的数肯定是1,然后第二小的数扔进去,显然就是他所在的行和列已经填过的最大数+1
这很容易想到。
但是相同的数怎么处理?
请用并查集,而不是扫个四五遍。
在同一行同一列的相同元素,就把这两个东西扔进同一个并查集,然后这些元素都是相同的。
那么这些值就等于这一坨所在的行列最大值+1就好了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
pair<int,pair<int,int> >A[maxn];
map<int,int>X;
map<int,int>Y;
int Hx[maxn],Hy[maxn];
int ans[maxn];
int fa[maxn];
int fi(int u){
return u != fa[u] ? fa[u] = fi( fa[u] ) : u;
}
void uni(int u ,int v){
int p1 = fi( u ) , p2 = fi( v );
if( p1 != p2 ) fa[p1] = p2;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<n*m;i++)
{
fa[i]=i;
scanf("%d",&A[i].first);
A[i].second.first=i/m;
A[i].second.second=i%m;
}
int j=-1;
sort(A,A+n*m);
for(int i=0;i<n*m;i++)
{
if(i!=n*m-1&&A[i].first==A[i+1].first)
continue;
for(int k=j+1;k<=i;k++)
{
int p = A[k].second.first*m+A[k].second.second;
int x = A[k].second.first,y=A[k].second.second;
Hx[x]=p;
Hy[y]=p;
}
for(int k=j+1;k<=i;k++)
{
int p = A[k].second.first*m+A[k].second.second;
int x = A[k].second.first,y=A[k].second.second;
uni(Hx[x],p);
uni(Hy[y],p);
}
for(int k=j+1;k<=i;k++)
{
int p = A[k].second.first*m+A[k].second.second;
int x = A[k].second.first,y=A[k].second.second;
int pa = fi(p);
ans[pa]=max(ans[pa],max(X[x],Y[y])+1);
}
for(int k=j+1;k<=i;k++)
{
int p = A[k].second.first*m+A[k].second.second;
int x = A[k].second.first,y=A[k].second.second;
X[x]=max(X[x],ans[fi(p)]);
Y[y]=max(Y[y],ans[fi(p)]);
}
j=i;
}
for(int i=0;i<n*m;i++)
{
printf("%d ",ans[fi(i)]);
if(i%m==m-1)printf("\n");
}
}
Codeforces Round #345 (Div. 2) E. Table Compression 并查集的更多相关文章
- Codeforces Round #345 (Div. 2) E. Table Compression 并查集+智商题
E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #345 (Div. 1) C. Table Compression (并查集)
Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorith ...
- Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集
题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...
- codeforces Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集
C. Table Compression Little Petya is now fond of data compression algorithms. He has already studied ...
- Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集
E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...
- Codeforces Round #345 (Div. 2) E. Table Compression(并查集)
传送门 首先先从小到大排序,如果没有重复的元素,直接一个一个往上填即可,每一个数就等于当前行和列的最大值 + 1 如果某一行或列上有重复的元素,就用并查集把他们连起来,很(不)显然,处于同一行或列的相 ...
- Codeforces Round #245 (Div. 2) B. Balls Game 并查集
B. Balls Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...
- Codeforces Round #603 (Div. 2) D. Secret Passwords 并查集
D. Secret Passwords One unknown hacker wants to get the admin's password of AtForces testing system, ...
- Codeforces Round #600 (Div. 2) D题【并查集+思维】
题意:给你n个点,m条边,然后让你使得这个这个图成为一个协和图,需要加几条边.协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边. 思路:通过并查集不断维护连通量的最大编号 ...
随机推荐
- mysql之数据库操作进阶(三)
环境信息 数据库:mysql-5.7.20 操作系统:Ubuntu-16.04.3 查询 条件查询 # 使用where关键字 select * from 表名 where 条件 # 比较运算符 > ...
- 【Python学习】程序运行完发送邮件提醒
有时候我们运行一个需要跑很长时间的程序,不管是在云主机还是本地主机上运行,我们都不可能一直守在电脑面前等.所以想到使用邮件来通知提醒. 示例代码如下 # -*- coding: utf-8 -*- # ...
- "Flags mismatch irq" register interrupt handler error
Question : When you see the log "Flags mismatch irq ............", maybe you use the same ...
- 在Github里集成Readthedocs服务
Readthedocs支持Markdown格式和sphinx格式的文档排版,是部署项目文档的绝佳平台.利用Github的托管服务,我们可以方便地将文档托管于Github,并利用Readthedocs查 ...
- 用Python写个自动ssh登录远程服务器的小工具
很多时候我们喜欢在自己电脑的终端直接ssh连接Linux服务器,而不喜欢使用那些有UI界面的工具区连接我们的服务器.可是在终端使用ssh我们每次都需要输入账号和密码,这也是一个烦恼,所以我们可以简单的 ...
- [How to]简单易用的拷贝Mac文件路径方法
效果: 在你想拷贝路径的文件夹或者文件上右键会出现 copy path 选项! 实现: 1.打开finder的的Automator组件 2.选择[服务]选项,点击[选取]按钮 3.搜索操作项目中[拷贝 ...
- [New learn]响应者链机制介绍
1.简介 测试代码库:https://github.com/xufeng79x/EventHandler 响应者链是系统寻找事件相应者的一个路径,他是同touch事件的Hit-testing过程具有 ...
- FineReport——登录不到决策系统
在不断的测试过程中,可能会造成缓存数据的累积,所以在登录过程中可能会出现登录不到决策系统,而是跳转到某一模板页面 解决方法就是清理缓存或者换一个浏览器测试.
- 【python】r+,w+ 全局变量
来源:http://www.educity.cn/wenda/352188.html r+:可读可写,若文件不存在,报错w+: 可读可写,若文件不存在,创建文本模式:遇换行符时根据操作系统不同自动转换 ...
- HDU 2829 Lawrence(四边形优化DP O(n^2))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...