Codeforces 650C 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 2
3 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.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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的更多相关文章
- Codeforces 650C Table Compression (并查集)
题意:M×N的矩阵 让你保持每行每列的大小对应关系不变,将矩阵重写,重写后的最大值最小. 思路:离散化思想+并查集,详见代码 好题! #include <iostream> #includ ...
- Codeforces 651E Table Compression【并查集】
题目链接: http://codeforces.com/problemset/problem/650/C 题意: 给定n*m的矩阵,要求用最小的数表示每个元素,其中各行各列的大小关系保持不变. 分析: ...
- 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 并查集
E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...
- 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 ...
- codeforces 651E E. Table Compression(贪心+并查集)
题目链接: E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- 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 ...
- CF650C Table Compression
CF650C Table Compression 给一个 \(n\times m\) 的非负整数矩阵 \(a\),让你求一个 \(n\times m\) 的非负整数矩阵 \(b\),满足以下条件 若 ...
- Code Forces 650 C Table Compression(并查集)
C. Table Compression time limit per test4 seconds memory limit per test256 megabytes inputstandard i ...
随机推荐
- js模拟手机触摸屏
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- kvm虚拟化管理平台WebVirtMgr部署-完整记录(安装Windows虚拟机)-(4)
一.背景说明 在之前的篇章中,提到在webvirtmgr里安装linux系统的vm,下面说下安装windows系统虚拟机的操作记录: 由于KVM管理虚拟机的硬盘和网卡需要virtio驱动,linux ...
- Netty开发UDP协议
UdpServer package org.zln.netty.five.part07; import io.netty.bootstrap.Bootstrap; import io.netty.ch ...
- cookies,sessionStorage和localStorage的区别
联系: sessionStorage和localStorage一样,都是用来缓存客户端缓存信息. 他们都只能存储字符串类型对象. 区别: localStorage的生命周期是永久的,除非用户主动清除浏 ...
- [tools]QuickPing
一款神器 quickping 能够很快的探测出该网断分出去哪些地址. 在线的会显示绿色 在线的+有主机名的显示为亮绿色
- Restful是什么,SOAP Webservice和RESTful Webservice
首先,应该怀着这样一种心态来学习Restful——Restful你可以将其理解一种软件架构风格,并且诠释了Http协议的设计初衷,所以不要把他理解的那么神秘,Restful风格有好处,当然也是有坏处的 ...
- zabbix(sql注入判断脚本)
zabbix(sql注入判断脚本) #-*-coding:utf-8-*- # code by anyun.org import urllib import re def getHtml(url): ...
- oracle文字与格式字符串不匹配的解决
oracle文字与格式字符串不匹配的解决 oracle的日期时间类型 在往oracle的date类型插入数据的时候,记得要用to_date()方法. 如insert into CUSLOGS(STAR ...
- 那些年我们写过的T-SQL(下篇)(转)
原文:http://www.cnblogs.com/wanliwang01/p/TSQL_Base04.html 下篇的内容很多都会在工作中用到,尤其是可编程对象,那些年我们写过的存储过程,有木有 ...
- JS闭包的理解
闭包的两个特点: 1.作为一个函数变量的一个引用 - 当函数返回时,其处于激活状态.2.一个闭包就是当一个函数返回时,一个没有释放资源的栈区. 其实上面两点可以合成一点,就是闭包函数返回时,该函数内部 ...