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条边,然后让你使得这个这个图成为一个协和图,需要加几条边.协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边. 思路:通过并查集不断维护连通量的最大编号 ...
随机推荐
- Shell脚本 - nginx启动脚本
OS:CentOS/Redhat 系列 并在 Centos 6.7 和 Centos 7.2 上测试正常 #!/bin/bash # # auth: daxin # time: 2018/07/10 ...
- ssh日常优化使用
config文件的使用 ssh命令默认会加载 ~/.ssh/config 文件作为配置文件,如果没有则采用默认配置.如果我们想要对ssh进行定制,那么就可以使用如下方法 [root@linux-nod ...
- 为什么Windows7打开项目的方式是灰的不能修改
http://jingyan.baidu.com/article/d3b74d64a964691f77e60900.html 进入组策略编辑器,即运行gpedit.msc,进入“用户配置”-“管理模板 ...
- Linux I2C(一)之常用的几种实例化(i2c_client ) 【转】
转自:http://blog.csdn.net/lugandong/article/details/48092397 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 前言 方式 ...
- CentOS在ssh下远程重装系统
CentOS在ssh下远程重装系统 http://www.zxsdw.com/index.php/archives/913/ 国外VPS服务器一般都有控制面板,有很多种系统可自行安装,但国内有些IDC ...
- c#使用selenium+Chromedriver参数配置
using System; //添加selenium的引用 using OpenQA.Selenium.PhantomJS; using OpenQA.Selenium.Chrome; using O ...
- SqlServer开启CLR使用(C#)DLL实现实时Socket通知
--1.默认情况下,SQL Server中的CLR是关闭的,所以我们需要执行如下命令打开CLR: reconfigure GO -- DROP FUNCTION dbo.fnScoketSend -- ...
- BZOJ 4516: [Sdoi2016]生成魔咒——后缀数组、并查集
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4516 题意 一开始串为空,每次往串后面加一个字符,求本质不同的子串的个数,可以离线.即长度为 ...
- signal, sigaction,信号集合操作
信号是与一定的进程相联系的,而建立其信号和进程的对应关系,这就是信号的安装登记. Linux主要有两个函数实现信号的安装登记:signal和sigaction.其中signal在系统调用的基础上实现, ...
- AC日记——[HNOI2008]GT考试 bzoj 1009
1009 思路: KMP上走DP(矩阵加速): DP[i][j]表示当前在第i位,同是匹配到不吉利串的第j位的方案数: 代码: #include <bits/stdc++.h> using ...