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条边,然后让你使得这个这个图成为一个协和图,需要加几条边.协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边. 思路:通过并查集不断维护连通量的最大编号 ...
随机推荐
- 关于搭建系统直播和Thinkphp的杂谈(持续更新)
Q:Access denied for user 'root'@'localhost' 错误 A:第一种:配置文件中把数据库的用户名密码再改一遍,把runtime里的文件删除 第二种:修改syste ...
- mysqlsla安装和使用介绍
安装mysqlsla源码路径:https://github.com/daniel-nichter/hackmysql.com源码存放路径:/usr/local/src1.获取源码如果没有git命令,请 ...
- 增加和减少mongodb复制集中的节点
MongoDB Replica Sets不仅提供高可用性的解决方案,同时也提供负载均衡的解决方案,增减 Replica Sets节点在实际应用中非常普通.例如,当应用的读压力暴增时,3台节点的环境已不 ...
- VirtualBox Network设置的NAT和Bridged Adapter模式区别
区别: NAT模式下,虚拟机仍然可以访问网络,但是从网络接收者的眼中看来,这些网络请求都来自宿主机,而感知不到虚拟机.外网也无法访问虚拟机网络.虚拟机和宿主机器的IP地址在不同的子网,比如192.16 ...
- FaceBook pop 动画开源框架使用教程说明
https://github.com/facebook/pop Pop is an extensible animation engine for iOS and OS X. In addition ...
- java实现中文或其他语言及标点符号等转换成unicode字符串,或unicode的16进制码转换回文字或符号等
package org.analysisitem20181016.test; public class Code128Test2019052201 { public static final Stri ...
- websphere7.0异常:SRVE0255E: 尚未定义要处理 /wcm 的 Web 组/虚拟主机
websphere7.0错误:SRVE0255E: 尚未定义要处理 /wcm 的 Web 组/虚拟主机. SRVE0255E: 尚未定义要处理 /wcm 的 Web 组/虚拟主机.SRVE0255E: ...
- VS打包软件部署------ClickOnce应用安装 (各版本.net引导文件安装,再发布文档离线安装下载地址)
一.1.其他引导程序包:地址 https://msdn.microsoft.com/zh-cn/vstudio/bb898654.aspx 2.离线安装各版本.net下载版(各种语言):https: ...
- 用事件队列解决GUI的操作顺序问题(Qt中处理方法)
GUI操作顺序问题引发异常: 有时候我们使用写GUI程序的时候会遇到这样的问题:比如在程序中,建立了一个列表的GUI.这个列表是随着时间不断更新的,而且操作也会读取这个列表GUI的内容. 如果这个程序 ...
- Java中的线程--线程中的工具
这主要想写一下Java中的jdk提供的一些线程中的工具, 一.semaphore信号灯 Semaphore可以维护当前访问自身的线程个数,并提供了同步机制,使用Semaphore可以控制同时访问资源的 ...