Codeforces Round #345 (Div. 2) E. Table Compression(并查集)
首先先从小到大排序,如果没有重复的元素,直接一个一个往上填即可,每一个数就等于当前行和列的最大值 + 1
如果某一行或列上有重复的元素,就用并查集把他们连起来,很(不)显然,处于同一行或列的相同元素始终应该保持一样的,然后再一个一个往上填
#include <bits/stdc++.h>
#define N 1000007
#define fi first
#define se second using namespace std; pair <int, pair<int, int> > A[N];
map <int, int> X, Y; int n, m;
int Hx[N], Hy[N], ans[N], f[N]; inline int find(int x)
{
return x == f[x] ? x : f[x] = find(f[x]);
} inline void uni(int x, int y)
{
x = find(x);
y = find(y);
if(x != y) f[x] = y;
} int main()
{
int i, j = -1, k, x, y, p;
scanf("%d %d", &n, &m);
for(i = 0; i < n * m; i++)
{
f[i] = i;
scanf("%d", &A[i].first);
A[i].se.fi = i / m;
A[i].se.se = i % m;
}
sort(A, A + n * m);
for(i = 0; i < n * m; i++)
{
if(i != n * m - 1 && A[i].fi == A[i + 1].fi) continue;
for(k = j + 1; k <= i; k++)
{
x = A[k].se.fi;
y = A[k].se.se;
p = A[k].se.fi * m + A[k].se.se;
Hx[x] = p;
Hy[y] = p;
}
for(k = j + 1; k <= i; k++)
{
x = A[k].se.fi;
y = A[k].se.se;
p = A[k].se.fi * m + A[k].se.se;
uni(Hx[x], p);
uni(Hy[y], p);
}
for(k = j + 1; k <= i; k++)
{
x = A[k].se.fi;
y = A[k].se.se;
p = A[k].se.fi * m + A[k].se.se;
p = find(p);
ans[p] = max(ans[p], max(X[x], Y[y]) + 1);
}
for(k = j + 1; k <= i; k++)
{
x = A[k].se.fi;
y = A[k].se.se;
p = A[k].se.fi * m + A[k].se.se;
p = find(p);
X[x] = max(X[x], ans[p]);
Y[y] = max(Y[y], ans[p]);
}
j = i;
}
for(i = 0; i < n * m; i++)
{
printf("%d ", ans[find(i)]);
if(i % m == m - 1) puts("");
}
return 0;
}
Codeforces Round #345 (Div. 2) E. Table Compression(并查集)的更多相关文章
- 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 ...
- 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 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 Round #345 (Div. 1) E. Clockwork Bomb 并查集
E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...
- Codeforces Round #245 (Div. 2) B. Balls Game 并查集
B. Balls Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...
- Codeforces Round #603 (Div. 2) D. Secret Passwords 并查集
D. Secret Passwords One unknown hacker wants to get the admin's password of AtForces testing system, ...
- Codeforces Round #600 (Div. 2) D题【并查集+思维】
题意:给你n个点,m条边,然后让你使得这个这个图成为一个协和图,需要加几条边.协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边. 思路:通过并查集不断维护连通量的最大编号 ...
随机推荐
- uvm_reg_sequence——寄存器模型(六)
寄存器模型 uvm_reg_sequence是UVM自带所有register sequence 的基类. 该类包含model, adapter, reg_seqr(uvm_sequencer). 感觉 ...
- 关于svn提交的时候强制写注释
本文固定链接: http://www.linuxyan.com/linux-service/229.html 转载请注明: admin 2012年09月29日 于 ㄨ销声匿迹.Linux 发表 在sv ...
- perl在linux下通过date获取当前时间
perl处理文件的时候最好添加上 处理的时间戳,获取系统的时间又多种方法,但是反引号是最原始的,不需要其他外界条件和lib的支持. my $now = `date "+%F %T" ...
- HDU 4274 Spy's Work (树形DP,模拟)
题意: 给定一棵树,每个节点代表一个员工,节点编号小的级别就小,那么点1就是boss了.接下来给出对m个点的限制,有3种符号分别是op=“大于/小于/等于”,表示以第i个点为根的子树所有人的工资之和 ...
- 在linux下面安装mysql 确认 配置文件路径 my.cnf
1.确认服务器my.cnf 文件路径.但不知道那个是 2.通过which mysql命令来查看mysql的安装位置: 3.通过/usr/local/mysql/bin/mysqld --verbose ...
- @AutoWired注解使用时可能会发生的错误
@Autowired注解:用于对Bean的属性变量.属性的set方法及构造函数进行标注,配合对应的注解处理器完成Bean的自动配置工作.@Autowired注解默认按照Bean类型进行装配. 1.在 ...
- ubuntu 16.0 利用ant编译 hadoop-eclipse-plugins2.6.0
折腾了两天,抱着不放弃的精神,我终于编译出我自己所需的hadoop中在eclipse中的插件 在网上下载的可能因为版本不一致,在编译的时候出现各种各样的问题,包括你的eclipse版本和hadoop版 ...
- Robot Framework(十) 执行测试用例——测试执行
3.2测试执行 本节描述如何执行从解析的测试数据创建的测试套件结构,如何在失败后继续执行测试用例,以及如何正常停止整个测试执行. 3.2.1执行流程 执行套房和测试 设置和拆卸 执行顺序 3.2.2继 ...
- python_113_socket编程
Socket语法及相关 socket概念 socket本质上就是在2台网络互通的电脑之间,架设一个通道,两台电脑通过这个通道来实现数据的互相传递. 我们知道网络 通信 都 是基于 ip+port 方能 ...
- 给 MSYS2 添加国内源
https://wiki.qt.io/MSYS2pacman -S base-devel git mercurial svn wget p7zip软件包 开发包 http://mirrors.ustc ...