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, zipalgorithms 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, jthen 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.

给一个n*m的矩阵,然后让你压缩一下,输出另外一个n*m的矩阵。

这两个矩阵要求在每一行每一列的大小关系保持不变。比如ai,j<ai,k那么第二个矩阵也得满足这个条件。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=1000000; int x[maxn+10],y[maxn+10],f[maxn+10],tmp[maxn+10];
int ansx[maxn+10],ansy[maxn+10];
struct node{
int r,l,val,id;
}ne[maxn+10]; bool cmp(node a,node b)
{
return a.val<b.val;
} bool cmpid(node a,node b)
{
return a.id<b.id;
} int Find(int i)
{
if(i!=f[i])
f[i]=Find(f[i]);
return f[i];
} int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
int cnt=0;
MM(tmp,0);
MM(x,0);MM(y,0);
MM(ansx,0);MM(ansy,0); for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
scanf("%d",&ne[++cnt].val);
ne[cnt].r=i;
ne[cnt].l=j;
ne[cnt].id=cnt;
f[cnt]=cnt;
} sort(ne+1,ne+cnt+1,cmp);
for(int k=1;k<=cnt;)
{
int s=k;
for(;ne[k].val==ne[s].val&&k<=cnt;k++);//数值相同的点
for(int i=s;i<k;i++)
{
int r=ne[i].r;
int l=ne[i].l; if(!x[r]) x[r]=i;
else {
int rr=Find(i);
f[rr]=Find(x[r]);
} if(!y[l]) y[l]=i;
else {
int rr=Find(i);
f[rr]=Find(y[l]);
}
} for(int i=s;i<k;i++)
{
int q=Find(i);
int r=ne[i].r,l=ne[i].l;
tmp[q]=max(tmp[q],max(ansx[r],ansy[l])+1);//至少要达到的数值
x[r]=y[l]=0;//清除,最终x,y全都为0,不影响后面
}//核心代码,因为数值从小到大遍历 for(int i=s;i<k;i++)
{
int q=Find(i);
int r=ne[i].r,l=ne[i].l;
ansx[r]=tmp[q];
ansy[l]=tmp[q];
}
} for(int i=1;i<=cnt;i++)
{
int q=Find(i);
ne[i].val=tmp[q];
} sort(ne+1,ne+cnt+1,cmpid); for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
printf("%d ",ne[(i-1)*m+j].val);
printf("\n");
}
}
return 0;
}

  分析:很有技术含量的一道题

1:首先将所有的数值从小到大排序,然后开始遍历,每次找出数值相同的点进行跟新,然后开始进行行与列的更新,不过因为同一行或列的点最后压缩出来的数值也得相同,所以需要利用并查集来合并,一同更新;

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

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

  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 Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集

    C. Table Compression Little Petya is now fond of data compression algorithms. He has already studied ...

  5. Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集

    E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...

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

    传送门 首先先从小到大排序,如果没有重复的元素,直接一个一个往上填即可,每一个数就等于当前行和列的最大值 + 1 如果某一行或列上有重复的元素,就用并查集把他们连起来,很(不)显然,处于同一行或列的相 ...

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

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

  9. Codeforces Round #600 (Div. 2) - D. Harmonious Graph(并查集)

    题意:对于一张图,如果$a$与$b$连通,则对于任意的$c(a<c<b)$都有$a$与$c$连通,则称该图为和谐图,现在给你一张图,问你最少添加多少条边使图变为和谐图. 思路:将一个连通块 ...

随机推荐

  1. 数组被遗忘的内置对象--》Array.find()

    需求:一个数组包含很多对象,对象中有很多属性.现在给你一个值,且这个值再这个数组的某个对象存在,那么如何找到这个对象? 首先想的是for循环遍历,但这样非常麻烦,js给我们提供了一个find()方法, ...

  2. PTA(Basic Level)1008.数组元素循环右移问题

    一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A0A1⋯AN−1)变换为(AN−M⋯AN−1A0A1⋯AN−M−1)(最 ...

  3. CSS3与页面布局学习总结——多种页面布局

    一.负边距与浮动布局 1.1.负边距 所谓负边距就是margin取负值的情况,如margin:-40px:margin-left:-100%.当一个元素与另一个元素margin取负值时将拉近距离.常见 ...

  4. Microsoft BarCode Control 16.0属性

    Labview(2018)可通过Active调用Microsoft BarCode Control 16.0来生成条形码, 参考资料如下: 生成效果: 二维码: 条形码: 执行程序发现修改线条宽度不影 ...

  5. POJ 2492 A Bug's Life 题解

    题面 这道题是一道标准的种类并查集: 种类并查集是给每个结点一个权值.然后在合并和查找的时候根据情况对权值来进行维护. 通过将原有的区间范围变大使并查集可以维护种类的联系: #include < ...

  6. python的类属性、实例属性、类方法、静态方法

    类属性 就像如下代码: class Person: name = "张三" # 共有类属性 __age = 18 # 私有类属性 在类中直接定义的属性就是类属性,它被所有的实例对象 ...

  7. Homebrew学习(一)之初认识

    Homebrew Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装.卸载.更新.查看.搜索等很多实用的功能.简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,会 ...

  8. 在easyui中解决使west和center为1:1,并且拖动窗口时能够自适应变化

    <script type="text/javascript"> // 解决页面两个grid的布局问题 $(function(){// 在页面加载完毕后 //consol ...

  9. Git命令的总结

    Git 是当前最流行的版本控制程序之一,文本包含了 Git 的一些基本用法 创建 git 仓库 初始化 git 仓库 mkdir project  # 创建项目目录cd project  # 进入到项 ...

  10. Redis5以上版本伪集群搭建(高可用集群模式)

    redis集群需要至少要三个master节点,我们这里搭建三个master节点,并且给每个master再搭建一个slave节点,总共6个redis节点,这里用一台机器(可以多台机器部署,修改一下ip地 ...