Codeforces 651E:Table Compression

43 篇文章 0 订阅
9 篇文章 0 订阅

E. Table Compression
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 gzbzzip 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.


题意是将给出的n*m的矩阵压缩,要求维持每一行每一列的大小关系都不变。

很有意思的一道题,对于每一个元素所在的行与列,对于相等的元素,用并查集维护其相等关系,将矩阵的元素从小到大排好序之后,对于每一个元素不断记录每一行每一列元素个数,更新其信息。

代码:

#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
using namespace std;
typedef long long ll;

#define INF 0x3fffffffffffffff

const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;

int n, m;
int *val[maxn], sz[maxn], fa[maxn], res[maxn];
int num_col[maxn], num_row[maxn];
vector< pair<int, int> >ha;

int getfa(int x)
{
	return x == fa[x] ? x : fa[x] = getfa(fa[x]);
}

void uni(int u, int v)
{
	u = getfa(u);
	v = getfa(v);
	if (sz[u] < sz[v])
	{
		swap(u, v);
	}
	fa[v] = u;
	sz[u] += sz[v];
}

void solve()
{
	int i, j, k;
	scanf("%d%d", &n, &m);

	for (i = 1; i <= n; i++)
	{
		val[i] = new int[m + 1];
	}
	for (i = 1; i <= m*n; i++)
	{
		fa[i] = i;
		sz[i] = 1;
	}
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			scanf("%d", &val[i][j]);
			ha.push_back(make_pair(val[i][j], (i - 1)*m + j));
		}
	}
	for (i = 1; i <= n; i++)
	{
		map<int, int>hax;
		for (j = 1; j <= m; j++)
		{
			if (hax.count(val[i][j]))
			{
				uni((i - 1)*m + j, hax[val[i][j]]);
			}
			else
			{
				hax[val[i][j]] = (i - 1)*m + j;
			}
		}
	}

	for (j = 1; j <= m; j++)
	{
		map<int, int>hax;
		for (i = 1; i <= n; i++)
		{
			if (hax.count(val[i][j]))
			{
				uni((i - 1)*m + j, hax[val[i][j]]);
			}
			else
			{
				hax[val[i][j]] = (i - 1)*m + j;
			}
		}
	}
	sort(ha.begin(), ha.end());
	int siz = ha.size();

	memset(num_col, 0, sizeof(num_col));
	memset(num_row, 0, sizeof(num_row));
	vector<int>equ;
	for (i = 0; i < siz; i++)
	{
		int se = ha[i].second;
		int v = ha[i].first;
		int row = ha[i].second / m + (ha[i].second % m == 0 ? 0 : 1);
		int col = ha[i].second%m + (ha[i].second % m == 0 ? m : 0);

		res[getfa(se)] = max(res[getfa(se)], max(num_row[row], num_col[col]) + 1);
		equ.push_back(se);

		if (i != siz - 1 && ha[i].first != ha[i + 1].first)
		{
			int sz_equ = equ.size();
			for (j = 0; j < sz_equ; j++)
			{
				int row = equ[j] / m + (equ[j] % m == 0 ? 0 : 1);
				int col = equ[j] % m + (equ[j] % m == 0 ? m : 0);
				num_row[row] = max(num_row[row], res[getfa(equ[j])]);
				num_col[col] = max(num_col[col], res[getfa(equ[j])]);
			}
			equ.clear();
		}
	}
	for (i = 1; i <= n*m; i++)
	{
		printf("%d%c", res[getfa(i)], (i%m == 0 ? '\n' : ' '));
	}
}

int main()
{

#ifndef ONLINE_JUDGE  
	freopen("i.txt", "r", stdin);
	freopen("o.txt", "w", stdout);
#endif 

	solve();

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值