time limit per test  4 seconds
memory limit per test  256 megabytes
input  standard input
output  standard output

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.

Examples
Input
2 2
1 2
3 4
Output
1 2
2 3
Input
4 3
20 10 30
50 40 30
50 60 70
90 80 70
Output
2 1 3
5 4 3
5 6 7
9 8 7
Note

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.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Solution:

这道题真心好,妙极。

Tutorial上给出的是图论向的解法,但我压根就没往那方向想过,never ever!

这里讨论一个正常人的解法。

将所有数从小到大排序,再往里填。我相信这是最自然的想法了。

紧接着而来的问题(trouble)就是按什么顺序填充那些相同的数。

我最开始的想法是很朴素也很naive的:

给排序加若干辅助的优先级。

这些辅助优先级是观察样例得来的猜想,当然不大可能靠得住。

-----------------------------------------------------------------------------------------

后来参考了某篇题解,解决了上面提到的trouble。

注意到题目中要求:such that the relative order of the elements in each row and each column remains the same.这句话其实是在表格元素上定义了一个等价关系

  a~b: a=b 且存在一条由a到b的曼哈顿路径且该路径上的所有点(元素)都属于集合{x | x=a}.

(这样描述看起来还是不够形式化 :D)

按这个等价关系可将表格(全集)分成若干等价类。

而题目正是要求压缩(映射)之后维持这个等价关系。

因此,实际上要在排序的基础上(对相同元素)进一步维护出等价类(equivelance classes)。

说到等价关系自然就想到并查集

---------------------------------------------------------------------------------------------------------------------------------------------

细节就不多说了,coding时自能体会(也许读者才不会像LZ,犯那样SB的错误呢)。

Implementation:

/*
In mathematics, an equivalence relation is a binary relation that is at the same time a
reflexive relation, a symmetric relation and a transitive relation. As a consequence of
these properties relation provides a partition of a set onto equivalence classes.
*/
#include <bits/stdc++.h>
using namespace std; const int N(1e6+); struct node{
int x, y, v;
bool operator <(const node &a)const{
return v<a.v;
}
}a[N], mx[N], my[N]; int par[N], ma[N], ans[N]; int m, n; int ID(node &a){
return a.x*m+a.y;
} int find(int x){
return x==par[x]?x:par[x]=find(par[x]);
} void unite(int x, int y){
x=find(x), y=find(y);
par[x]=y;
ma[y]=max(ma[y], ma[x]);
} int main(){
cin>>n>>m; for(int i=; i<n; i++){
for(int j=; j<=m; j++){
int x;
cin>>x;
a[i*m+j]={i, j, x};
}
} for(int i=; i<=m*n; i++)
par[i]=i; sort(a+, a+m*n+); //two-pointers
for(int i=, j; i<=m*n; ){
// cout<<a[i].v<<endl;
for(j=i; j<=m*n && a[j].v==a[i].v; j++){ ma[ID(a[j])]=max(ans[ID(mx[a[j].x])], ans[ID(my[a[j].y])]); //error-prone if(a[j].v==mx[a[j].x].v){
unite(ID(a[j]), ID(mx[a[j].x]));
}
else{
mx[a[j].x]=a[j];
} if(a[j].v==my[a[j].y].v){
unite(ID(a[j]), ID(my[a[j].y]));
}
else{
my[a[j].y]=a[j];
}
} for(; i!=j; i++){
int id=ID(a[i]);
// cout<<i<<' '<<find(ID(a[i]))<<endl;
// ma[ID(a[i])]=ma[find(ID(a[i]))]+1 //OMG!
ans[id]=ma[find(id)]+;
}
} for(int i=; i<n; i++){
for(int j=; j<=m; j++)
cout<<ans[i*m+j]<<' ';
cout<<'\n';
} return ;
}

---------------------------------------

其实还有许多可总结的,累了,坑留着往后再填吧。

Codeforces 650C Table Compression的更多相关文章

  1. Codeforces 650C Table Compression (并查集)

    题意:M×N的矩阵 让你保持每行每列的大小对应关系不变,将矩阵重写,重写后的最大值最小. 思路:离散化思想+并查集,详见代码 好题! #include <iostream> #includ ...

  2. Codeforces 651E Table Compression【并查集】

    题目链接: http://codeforces.com/problemset/problem/650/C 题意: 给定n*m的矩阵,要求用最小的数表示每个元素,其中各行各列的大小关系保持不变. 分析: ...

  3. Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集

    题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...

  4. Codeforces Round #345 (Div. 2) E. Table Compression 并查集

    E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...

  5. 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 ...

  6. codeforces 651E E. Table Compression(贪心+并查集)

    题目链接: E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  7. 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 ...

  8. CF650C Table Compression

    CF650C Table Compression 给一个 \(n\times m\) 的非负整数矩阵 \(a\),让你求一个 \(n\times m\) 的非负整数矩阵 \(b\),满足以下条件 若 ...

  9. Code Forces 650 C Table Compression(并查集)

    C. Table Compression time limit per test4 seconds memory limit per test256 megabytes inputstandard i ...

随机推荐

  1. S2小测--索引--视图

    1.  关于Sql server 视图 视图是一个虚拟表,我们在查询视图的时候,实际上是对基础表的查询.视图不仅可以作为SELECT查询的目标,也可以作为修改语句的目 标.理论上它可以像普通的物理表一 ...

  2. 从0开始学Java——JSP&Servlet——如何部署web应用程序

    web容器要求应用程序部署时,需要像下面这样组织其目录结构: 手动去创建这样的目录结构还是挺麻烦的,所幸我们有开发工具,所以可以像下面这样来部署一个web项目. 1)确认程序代码已经完成: 2)在ec ...

  3. [CareerCup] 5.8 Draw Horizonatal Line 画横线

    5.8 A monochrome screen is stored as a single array of bytes, allowing eight consecutive pixels to b ...

  4. js浮点数精确计算(加、减、乘、除)

    <SPAN style="FONT-SIZE: 18px">//说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显.这个函数返回较为精确的加 ...

  5. linux内核分析 第七周

    一.课堂相关 (一)预处理.编译.链接和目标文件的格式 1.可执行程序是怎么得来的 C代码--预处理--汇编代码--目标代码--可执行文件 预处理负责把include的文件包含进来及宏替换工作. he ...

  6. 用CNTK搞深度学习 (二) 训练基于RNN的自然语言模型 ( language model )

    前一篇文章  用 CNTK 搞深度学习 (一) 入门    介绍了用CNTK构建简单前向神经网络的例子.现在假设读者已经懂得了使用CNTK的基本方法.现在我们做一个稍微复杂一点,也是自然语言挖掘中很火 ...

  7. vector 内存释放问题

    关于容器的处理,只是拿来用,理解不深,但是今天跑程序出了问题.释放空间未得到真正的释放.于是网上找了一些文章,解决的问题. 解决方法:使用swap 加上clear,一起释放空间. 原理:即先创建一个临 ...

  8. 喝咖啡写脚本,顺便再加一点点CSS语法糖 2.五分钟学会Less

    CoffeeScript + Html5 + Less这个新组合,看上去Less更容易拿下,先尝尝糖吧. Less这么小个东西,竟然要FQ,真是没有天理,简直不可理喻,先不管那么多了,那就看这个吧.h ...

  9. 第三十课:JSDeferred详解1

    本课难度非常大,看一遍,蛋会疼,第二遍蛋不舒服,第三遍应该貌似懂了.初学者莫来,没必要,这完全就是一个研究. JSDeferred是日本高手cho45搞出来的,其易用性远胜于Mochikit Defe ...

  10. iOS UI基础-17.0 UILable之NSMutableAttributedString

    在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦 ...