codeforces Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集
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.
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 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.
2 2
1
4
1 2
2 3
4 3
20 10 30
50 40 30
50 60 70
90 80 70
2 1 3
5 4 3
5 6 7
9 8 7
In the first sample test, despite the fact a1, 2 ≠ a21, they are not located in the same row or column so they may become equal after the compression.
题意:个数不超过1e6个数的二维数列;按照行与列数的相对大小尽可能的缩小为正整数,但不在同一行或同一列的数的缩放前后的大小没有关系;
输出缩放后的数列;
思路:排序后每次处理都是处理值相等的一串数据,并且是看成没没有填入到新数组中,这样使用并查集就可以得到“十”字形相等的根节点的最大值,即所有这棵并查集下的节点的值;x[i],y[i]来模拟并查集,X[],Y[]表示行列上一个值填到的数值,所以之后直接得到根节点所要填入的值;
#include<bits/stdc++.h>
using namespace std;
int i,j,k,n,m,T,tot;
const int N = ;
struct data{
int r,c,v,id;
}p[N];
bool cmp(const data &a,const data &b){return a.v < b.v;}
int f[N],X[N],Y[N],ans[N],x[N],y[N],tmp[N];
int Find(int a){return a==f[a]?f[a]:f[a]=Find(f[a]);}
int main()
{
scanf("%d%d",&n,&m);
for(i = ;i <= n;i++)
for(j = ;j <= m;j++){
scanf("%d",&p[++tot].v);
p[tot].r = i,p[tot].c = j;
p[tot].id = tot;
f[tot] = tot;
}
sort(p+,p++tot,cmp);
for(i = ;i <= tot;i = j){
for(j = i;p[i].v == p[j].v;++j);
for(k = i;k < j;k++){
int r = p[k].r, c = p[k].c;
if(!x[r]) x[r] = k;// 行并查
else f[Find(k)] = Find(x[r]);
if(!y[c]) y[c] = k;
else f[Find(k)] = Find(y[c]);//f[k]会因为十字型交叉而出错;
}
for(k = i;k < j;k++){//只是在之前的值的基础上得到,不是模拟填入值
int q = Find(k);
tmp[q] = max(tmp[q],max(X[p[k].r],Y[p[k].c])+);
}
for(k = i;k < j;k++){//根节点得到的是全体的值
x[p[k].r] = y[p[k].c] = ;
X[p[k].r] = Y[p[k].c] = ans[p[k].id] = tmp[Find(k)];
}
}
for(i = ;i <= tot;i++){
printf("%d ",ans[i]);
if(i%m == ) puts("");
}
}
codeforces Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集的更多相关文章
- 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 Round #345 (Div. 2) E. Table Compression(并查集)
传送门 首先先从小到大排序,如果没有重复的元素,直接一个一个往上填即可,每一个数就等于当前行和列的最大值 + 1 如果某一行或列上有重复的元素,就用并查集把他们连起来,很(不)显然,处于同一行或列的相 ...
- Codeforces Round #345 (Div. 2) E. Table Compression 并查集
E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...
- 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 #346 (Div. 2) F. Polycarp and Hay 并查集 bfs
F. Polycarp and Hay 题目连接: http://www.codeforces.com/contest/659/problem/F Description The farmer Pol ...
- Codeforces Round #181 (Div. 2) B. Coach 带权并查集
B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集
http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...
- Codeforces Round #363 (Div. 2) D. Fix a Tree —— 并查集
题目链接:http://codeforces.com/contest/699/problem/D D. Fix a Tree time limit per test 2 seconds memory ...
随机推荐
- CCCatmullRomTo&CCCatmullRomBy
注: 云形线(Catmull-Rom curve曲线) 云线(Spline或B-spline)在数学上有很多种类,常用的三阶云线有Hermite, Bezier, Uniform B-spline, ...
- Middleware
Middleware The middleware gives a single shot to the views associated into Controllers, before execu ...
- c语言实例
#include <stdio.h> int main() { ; ; i=max(j,k); printf("i=%d\n",i); ; } int max(int ...
- mac mysql error You must reset your password using ALTER USER statement before executing this statement.
安装完mysql 之后,登陆以后,不管运行任何命令,总是提示这个 step 1: SET PASSWORD = PASSWORD('your new password'); step 2: ALTER ...
- 《Mysql 公司职员学习篇》 第一章 小A的烦恼
第一章 小A的烦恼 ----- 为什么学习数据库 和 如何选择数据库 小A是某公司的职员,公司数据部的员工,平常的大小工作,完全离不开EXCELL,而最近小A却越来越苦恼,不由的向好朋友小Y吐槽.小 ...
- vsftpd给root设置访问权限
1:Linux下安装vsftpd之后,默认的配置是匿名用户可以登录,匿名帐户有两个:用户名:anonymous密码:空 用户名:ftp密码:ftp 2:如果要用匿名进行上传删除等操作需要配置其它参数. ...
- bootstrap兼容IE
这种响应式的布局正是通过CSS3的媒体查询(Media Query)功能实现的,根据不同的分辨率来匹配不同的样式.IE8浏览器并不支持这一优秀的Css3特性,Bootstrap在开发文档中写了如何使用 ...
- Lua - 基础语法
Hello World 交互式编程 Lua 交互式编程模式可以通过命令 lua -i 或 lua 来启用: [huey@huey-K42JE lua]$ lua Lua 5.1.4 Copyright ...
- VS2012无法创建项目:未找到与约束……匹配的导出
故障情况:7月10号后用VS2012创建项目时,弹出如下对话框,无法创建新项目: 而后经网络搜索确定是7月10号更新了系统补丁后造成的 解决方案: 1.卸载这两个补丁后重启电脑: 2.到http:// ...
- ubuntu系统安装flashplayer
打开浏览器,输入adobe flashplayer 进入官方网站,下载Linux 32-bit, 简体中文, Firefox,下载.tar.gz包. 然后点击立即下载.下载之后找到解压该文件夹,找到 ...