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条边,然后让你使得这个这个图成为一个协和图,需要加几条边.协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边. 思路:通过并查集不断维护连通量的最大编号 ...
随机推荐
- 离线安装SDK
1.下载android-sdk_rXX-windows.zip(XX为版本号,也可以下.exe版的不过没试过) 2.下载SDK 2.1.在浏览器输入http://dl-ssl.google.com/a ...
- scrapy再学习与第二个实例
这周对于Scrapy进一步学习,知识比较零散,需要爬取的网站因为封禁策略账号还被封了/(ㄒoㄒ)/~~ 一.信息存储 1.log存储命令:scrapy crawl Test --logfile=tes ...
- artDialog的一些例子与一些属性的介绍。
1.支持自定义按钮 var dialog = art.dialog({ title: '警告', content: '点击管理按钮将让删除按钮可用', width: '20em', button: [ ...
- [设计模式-行为型]模板方法模式(Template Method)
一句话 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中. 概括
- 监测mysql错误日志,有错误自动邮件报警
监测mysql错误日志,有错误自动邮件报警 http://blog.csdn.net/yabingshi_tech/article/details/51443401 MySQL:监控慢日志.错误日志. ...
- bzoj 1449 费用流
思路:先把没有进行的场次规定双方都为负,对于x胜y负 变为x + 1胜 y - 1 负所需要的代价为 2 * C[ i ] * x - 2 * D[ i ] * y + C[ i ] + D[ i ...
- 洛谷P1940买蛋糕
题目传送门 题意:给定你一个数n,要求用最小个数的整数组成小于等于n的所有整数,并求出方案数. 很明显,擅长二进制的大犇们肯定一眼就看得出方案数是log2(n)+1,然而我并不擅长,但是推了一小会儿也 ...
- bytes2HexString
public static String bytes2HexString(byte[] b) { String r = ""; for (int i = 0; i < b.l ...
- 解决PHPExcel长数字串显示为科学计数
在excel中如果在一个默认的格中输入或复制超长数字字符串,它会显示为科学计算法,例如身份证号码,解决方法是把表格设置文本格式或在输入前加一个单引号. 使用PHPExcel来生成excel,也会遇到同 ...
- Python标准库:内置函数divmod(a, b)
本函数是实现a除以b,然后返回商与余数的元组. 如果两个参数a,b都是整数,那么会采用整数除法,结果相当于(a//b, a % b).如果a或b是浮点数,相当于(math.floor(a/b), a% ...