Codeforces 651E Table Compression【并查集】
题目链接:
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【并查集】的更多相关文章
- Codeforces 650C Table Compression  (并查集)
		题意:M×N的矩阵 让你保持每行每列的大小对应关系不变,将矩阵重写,重写后的最大值最小. 思路:离散化思想+并查集,详见代码 好题! #include <iostream> #includ ... 
- Codeforces Round #345 (Div. 2) E. Table Compression 并查集
		E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ... 
- 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 ... 
- 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 ... 
- Code Forces 650 C  Table Compression(并查集)
		C. Table Compression time limit per test4 seconds memory limit per test256 megabytes inputstandard i ... 
- Codeforces 650C Table Compression
		传送门 time limit per test 4 seconds memory limit per test 256 megabytes input standard input output st ... 
- Codeforces Gym 100463E Spies 并查集
		Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ... 
- Codeforces 859E Desk Disorder 并查集找环,乘法原理
		题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ... 
- Codeforces - 828C String Reconstruction —— 并查集find()函数
		题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ... 
随机推荐
- linux小白成长之路11————linux命令大全
			1. 启动,关机,登入,登出相关命令 登录:login 登出:logout 登出:exit 停止系统:shutdown 停止系统:halt 重启动:reboot 切断电源:poweroff 把内存里的 ... 
- poj2886 Who Gets the Most Candies?
			思路: 先打反素数表,即可确定因子最多的那个数.然后模拟踢人的过程确定对应的人名.模拟的过程使用线段树优化加速. 实现: #include <cstdio> #include <cs ... 
- EL表达式、JSTL
			EL表达式 一.简介 > JSP表达式 <%= %> 用于向页面中输出一个对象. > 到JSP2.0时,在我们的页面中不允许出现 JSP表达式和 脚本片段. ... 
- Android开发中查看未root真机的app数据库
			在Android开发中,如果用到数据库来储存数据,那么难免就要查看数据库中的内容,可是对于未root的真机来说,查看数据库就不是那么容易了,如果仅仅为了查看数据库再把手机root了,有点得不偿失,所以 ... 
- APP崩溃处理
			以前经常遇到APP内部异常情况下的Exception,最初是通过try catch这样的方式处理:但是APP上线后,用户在特地的情况下触发 了某些Exception,当然这些Exception从理论和 ... 
- js阻塞ui进程涉及的知识点整理
			项目进行中遇到了同步ajax阻塞ui线程阻塞的问题,原因是执行两个同步ajax请求为一次完整的方法,因业务需求需要循环执行这个方法,检查后台返回的数据正确,但是由于ajax请求时间过长,考虑增加遮罩层 ... 
- 数据字典Dictionary存放键值对
			1. 方法思路: 使用数据字典[Dictionary<string, string>],声明一个list集合,将“XML子节点名称”.“节点值”以键[节点名称]值[节点值]对的形式 ... 
- Asp.Net 设计模式 之 “特殊”的单例模式
			特殊的单例模式 要点在这里,提前预览: public SingleDemo() { name = "yy"; age = 20; //特殊的单例,this指代得失当前的Single ... 
- 框架开发之Java注解的妙用
			注解的好处:1.能够读懂别人写的代码,特别是框架相关的代码.2.本来可能需要很多配置文件,需要很多逻辑才能实现的内容,就可以使用一个或者多个注解来替代,这样就使得编程更加简洁,代码更加清晰.3.(重点 ... 
- image和TFRecord互相转换
			关说不练假把式.手上正好有车牌字符的数据集,想把他们写成TFRecord格式,然后读进来,构建一个简单的cnn训练看看.然后发现准确率只有0.0x.随机猜也比这要好点吧.只能一步步检查整个过程.暂时想 ... 
