Codeforces Round #345 (Div. 1) C. Table Compression
来源:互联网 发布:邮箱大师mac下载 编辑:程序博客网 时间:2024/05/22 12:52
Little Petya is now fond of data compression algorithms. He has already studiedgz,bz,zip algorithms and many others. Inspired by the new knowledge, Petya is now developing the new compression algorithm which he wants to namedis.
Petya decided to compress tables. He is given a table a consisting ofn rows andm columns that is filled with positive integers. He wants to build the tablea' 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 rowi of the initial tableai, j < ai, k, then in the resulting tablea'i, j < a'i, k, and ifai, j = ai, k thena'i, j = a'i, k. Similarly, if in some columnj of the initial tableai, j < ap, j then in compressed tablea'i, j < a'p, j and ifai, j = ap, j thena'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 andm (, 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 containingm 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 21 23 4
1 22 3
4 320 10 3050 40 3050 60 7090 80 70
2 1 35 4 35 6 79 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.
按原权值排序后贪心的从小的开始标号,最小的一定编为1,然后按顺序把剩下的数字依次重新编号,权值相同且可以通过行列间接直接连接在一起的数字在新表里的编号也
相同,可以通过并查集合并这些数字并保留其中最大的编号,写了一次就过了,然而代码依然写的又臭又长。
#include <cstdio>#include <algorithm>#include <iostream>using namespace std;int tot,n,m,x,h[1000002],l[1000002],f[1000002],hv[1000002],lv[1000002],val[1000002];struct point{int x,y,v;int num(){return (x-1)*m+y;}} a[1000002];int find(int k){if(f[k] == k) return k;f[k]=find(f[k]);return f[k];}bool camp(point a,point b){return a.v<b.v;}void insert(int i,int j,int v){tot++;a[tot].x=i;a[tot].y=j;a[tot].v=v;}int main(){cin.sync_with_stdio(false);cin>>n>>m;for(int i=1;i <= n;i++) for(int j=1;j <= m;j++) { cin>>x; insert(i,j,x); }sort(a+1,a+1+n*m,camp);for(int i=1;i <= n*m;i++) f[i]=i;int now=1;for(int i=1;i <= n*m+1;i++){if(a[i].v != a[i-1].v){int s=now,t=i-1;if(s <= t){for(int j=s;j <= t;j++){val[a[j].num()]=max(hv[a[j].x],lv[a[j].y])+1;if(h[a[j].x] == 0){h[a[j].x]=a[j].num();}else {if(val[find(h[a[j].x])] < val[find(a[j].num())]){val[f[h[a[j].x]]]=val[f[a[j].num()]];}f[f[a[j].num()]]=f[h[a[j].x]];}if(l[a[j].y] == 0){l[a[j].y]=a[j].num();}else {if(val[find(l[a[j].y])] < val[find(a[j].num())]){val[f[l[a[j].y]]]=val[f[a[j].num()]];}f[f[a[j].num()]]=f[l[a[j].y]];}}for(int j=s;j <= t;j++){val[a[j].num()]=val[find(a[j].num())];hv[a[j].x]=lv[a[j].y]=val[a[j].num()];h[a[j].x]=l[a[j].y]=0;}}now=i;}}for(int i=1;i <= n*m;i++){if(i % m == 0) {cout<<val[i]<<endl;}else cout<<val[i]<<" ";} }
- Codeforces Round #345 (Div. 1) C. Table Compression
- Codeforces Round #345 (Div. 1) C. Table Compression
- Codeforces Round #345 (Div. 1) C. Table Compression (并查集)
- Codeforces Round #345 (Div. 2) E. Table Compression
- 【Codeforces Round 345 (Div 1) C】【并查集缩环+拓扑最长路】Table Compression nm矩形权值缩小大小关系不变
- Codeforces Round #345 (Div. 2) E. Table Compression(并查集)★ ★
- Codeforces 650C Table Compression
- Codeforces #345 div1 C. Table Compression 并查集 构造
- Codeforces Round #397(Div. 1 + Div. 2 combined)C. Table Tennis Game 2【贪心】
- Codeforces Round #273 (Div. 2) --C Table Decorations
- Codeforces Round #273 (Div. 2) C. Table Decorations
- Codeforces Round #273 (Div. 2) C Table Decorations
- Codeforces Round #273 (Div. 2) C.Table Decorations 贪心
- Codeforces Round #311 (Div. 2) C. Arthur and Table
- Codeforces Round #273 (Div. 2) C - Table Decorations dp
- Codeforces Round #311 (Div. 2) C. Arthur and Table
- Codeforces Round #323 (Div. 2) C.GCD Table
- Codeforces Round #323 (Div. 2)C. GCD Table
- 加载等待特效之ProgressDialog使用总结
- 关于mysql和pgsql中一些常用的函数
- 第二周上机实践项目 项目3-小试循环--(3)(4)(5)
- Python图像处理库PIL中图像格式转换(一)
- Java 序列化浅析
- Codeforces Round #345 (Div. 1) C. Table Compression
- 第一次上机实验-1
- 项目3-小试循环
- 在Jetson TK1上安装ROS(机器人操作系统)
- 第二周项目1-宣告主权
- Android 实用案例--Shape绘制实用圆圈
- svn忽略target
- RMAN恢复简单实验
- 我已经是CSDN博主了 嘿嘿..