题目链接:

http://codeforces.com/problemset/problem/650/C

题意:

给定n*m的矩阵,要求用最小的数表示每个元素,其中各行各列的大小关系保持不变。

分析:

将所有元素从小到大排序,然后找到每个元素相应位置进行填充,由于题目要求是每行每列的大小关系保持不变,所以填充的元素应为所在行和所在列中最大元素+1,保存好各行各列当前的最大值,并根据最后填充的元素不断更新就好啦。

问题是如何处理相同元素以及他们所在行列的更新。

这里使用并查集,将相同元素值的位置保存在一个并查集中,这样就可以保证相同元素在新的矩阵中仍然相等~~~使用并查集新姿势get

代码:

#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm>
#include<stack>
using namespace std;
const int maxn = 1000005;
int pa[maxn], _rank[maxn];
int v[maxn], row[maxn], cal[maxn], nv[maxn];
int m, n;
typedef pair<int, int>pii;
pii p[maxn];
#define fi first
#define se second
void init()
{
for(int i = 0; i < n * m; i++)
pa[i] = i;
}
int _find(int x)
{
if(pa[x]==x) return x;
else return pa[x] = _find(pa[x]);
}
void unite(int x, int y)
{
int rx = _find(x), ry = _find(y);
if(rx == ry) return;
if(_rank[rx]>_rank[ry]) pa[ry]=rx;
else {
pa[rx] = ry;
if(_rank[rx]==_rank[ry]) _rank[ry]++;
}
return;
}
bool same(int x, int y)
{
return _find(x)==_find(y);
}
int main (void)
{
scanf("%d%d", &n, &m);
for(int i = 0; i < n * m; i++){
scanf("%d",&v[i]);
p[i] = pii(v[i], i);
}
init();
for(int i = 0; i < n; i++){
map<int, int>tmp;
for(int j = 0; j < m; j++){
if(tmp.count(v[i * m + j])){
unite(tmp[v[i * m + j]], i * m + j);
}else
tmp[v[i * m + j]] = i * m + j;
}
}
for(int j = 0; j < m; j++){
map<int, int>tmp;
for(int i = 0; i < n; i++){
if(tmp.count(v[i * m + j])){
unite(tmp[v[i * m + j]], i * m + j);
}else
tmp[v[i * m + j]] = i * m + j;
}
}
sort(p, p + n * m);
stack<int>tmp;
for(int i = 0; i < n * m; i++){
int id = p[i].se;
int x = id / m, y = id % m;
nv[_find(id)] = max(nv[_find(id)], max(row[x], cal[y]) + 1);
tmp.push(id);
if(p[i].fi !=p[i + 1].fi){
while(!tmp.empty()){
id =tmp.top(); tmp.pop();
x = id / m, y = id % m;
row[x] = nv[_find(id)];
cal[y] = nv[_find(id)];
}
}
}
for(int i = 0; i < n * m; i++)
printf("%d%c", nv[_find(i)], (i + 1)%m == 0?'\n':' '); return 0;
}

好吧,我真是弱得cry,想了好久。。。

Codeforces 651E Table Compression【并查集】的更多相关文章

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

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

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

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

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

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

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

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

  6. Codeforces 650C Table Compression

    传送门 time limit per test 4 seconds memory limit per test 256 megabytes input standard input output st ...

  7. Codeforces Gym 100463E Spies 并查集

    Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...

  8. Codeforces 859E Desk Disorder 并查集找环,乘法原理

    题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...

  9. Codeforces - 828C String Reconstruction —— 并查集find()函数

    题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...

随机推荐

  1. 【深入.NET平台】浅谈.NET Framework基元类型

    什么是基元类型? 初学者可能很少听说过这个名词,但是平时用得最多的肯定是基元类型.先看下面两行代码: System.Int32 a = ; ;  上面两行代码都表示声明一个int类型的变量,但在平时写 ...

  2. [BZOJ1088][SCOI2005]扫雷Mine DP

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1088 记录下每一个格子对应左边格子放的雷的情况,然后dp转移就好了. #include&l ...

  3. orcale 数据库的一些知识

    最近学了一些Oracle数据库的知识,我想自己整理一下,以后也方便自己查阅的. orcale 数据库登录(tiger) 1. sql plus 登录 用户名: sys 口令: 主机字符串:orcl a ...

  4. mysql 判断null 和 空字符串

    1.在mysql中null 不能使用任何运算符与其他字段或者变量(函数.存储过程)进行运算.若使用运算数据就可能会有问题. 2.对null 的判断: 创建一个user表:id 主健 name 可以为空 ...

  5. Godaddy域名301跳转问题处理

    前言:Godaddy的域名301跳转一共有六步,详情见以下步骤: 第一步: 第二步:找到你的域名,并点击DNS 第三步:点击添加 第四步:添加解析ip地址 第五步:域名转址,也就是301跳转 第六步: ...

  6. java格式化sql

    在日志分析中,经常会对记录的sql进行分析,所以将一整行sql格式化,进行多行缩就显得很有必要,许多数据库客户端都提供sql的格式化功能,但复杂的多层嵌套sql往往格式化的l还不够友好,所以就自己造了 ...

  7. JavaScript——responseType

    https://www.cnblogs.com/cdemo/p/5225848.html https://blog.csdn.net/wkyseo/article/details/78232485 异 ...

  8. [转] 以超级管理员身份运行bat

    (转自:以超级管理员身份运行bat - lishirong   原文日期:2013.07.04) 废话不多说,直接上代码: -------------------------------------- ...

  9. 【分享】4412开发板POP烧写ubuntu出错,如何挂载emmc分区解决方法

    本文转自:http://bbs.topeetboard.com 平台:4412精英版系统:ubuntu系统 按照教程烧写ubuntu文件系统,TF卡和EMMC分区都完成(总之之前的操作试了几遍都是没问 ...

  10. oracle 表之间的连接

    排序 - - 合并连接(Sort Merge Join, SMJ): a) 对于非等值连接,这种连接方式的效率是比较高的. b) 如果在关联的列上都有索引,效果更好. c) 对于将2个较大的row s ...