传送门

首先先从小到大排序,如果没有重复的元素,直接一个一个往上填即可,每一个数就等于当前行和列的最大值 + 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(并查集)的更多相关文章

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

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

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

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

  4. Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集

    题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...

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

  6. Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集

    E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...

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

  8. 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, ...

  9. Codeforces Round #600 (Div. 2) D题【并查集+思维】

    题意:给你n个点,m条边,然后让你使得这个这个图成为一个协和图,需要加几条边.协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边. 思路:通过并查集不断维护连通量的最大编号 ...

随机推荐

  1. 解决更新到os x10.11后openssl头文件无法找到的问题

    os x从10.10更新到10.11后,原有代码编译报错,#include <openssl/ssl.h>等头文件无法找到: "openssl/ssl.h: No such fi ...

  2. UVA 11374 Airport Express (最短路)

    题目只有一条路径会发生改变. 常见的思路,预处理出S和T的两个单源最短路,然后枚举商业线,商业线两端一定是选择到s和t的最短路. 路径输出可以在求最短路的同时保存pa数组得到一棵最短路树,也可以用di ...

  3. idea 发布和本地测试问题

    1.maven本地打包成jar 提示[错误: 找不到或无法加载主类]修改 配置maven ---->Runner---->VM Optins [-DarchetypeCatalog=loc ...

  4. Windows MinGW 64-bit boost 踩坑

    >g++ -Wall -shared -g -DBUILD_DLL main.cpp -ID:\gcc\boost\include\boost-1_69 -LD:\gcc\boost\lib - ...

  5. shell脚本,一个经典题目。

    [root@localhost wyb]# cat zhuijiu.sh #!/bin/bash #.写一个脚本执行后,输入名字,产生随机数01-99之间的数字. #.如果相同的名字重复输入,抓到的数 ...

  6. awk日志分割

    awk日志分割 1. awk实现日志按照日期分割 #!/bin/bash DATE=$(date -d yesterday +%Y-%m-%d) awk  'BEGIN{RS="'$DATE ...

  7. linux-MySQL基本指令-增删改查

    常用指令 指令作用 指令 查看/查询 show,select,desc 创建 create 删除 drop,delete,truncate 切换/进入 use 添加记录 insert 查询类 查看数据 ...

  8. netfilter 和 iptables

    http://blog.chinaunix.net/uid/23069658/cid--1-list-4.html 洞悉linux下的Netfilter&iptables  系列,有一到十六, ...

  9. javascript 使用 load 和 unload 事件,解决浏览器打开和关闭时需要做的操作。

    最近有一个业务,就是修改一个业务需要加上锁.也就是打开浏览器时,加锁.等用户操作完毕,关掉浏览器之后在把锁打开.一开始想问题很局限.只是想着,关闭浏览器解锁,刷新页面不做操作.然后就一直在找在调用un ...

  10. 使用 HTML5 Geolocation 构建基于地理位置的 Web 应用学习网站分享

    HTML5 中的新功能 HTML5 是最新一代的 HTML 规范,是 W3C 与 WHATWG 合作的结果,目前仍外于开发中.自从上一代 HTML4,Web 世界已经发生了巨大的变化,HTML5 的到 ...