题意:给你一个n*m的矩阵,需要在不改变每一行和每一列的大小关系的情况下压缩一个矩阵,压缩后的矩阵所有数的总和尽量的小。

思路:我们有这样的初步设想:对于在一行或一列的数x,y,若x<y,则建立一条x的位置到y的位置的边。之后进行拓扑排序的DP即可。然而会被卡边数卡掉,所以需要其它的解法。

新思路:我们把所有的数排个序,这样方便选对所有相同的数赋值。我们从小到大对所有的数赋值,合并这个数所在的行和列,选取相关的行和列中的最大值+1作为作为这个数的新值。

为什么这样做正确呢?可以类比拓扑排序的dp过程,我们所赋的新值不能破坏原来行和列的大小关系,所以要找相关的行和列中的最大值+1。用并查集可以快速判断哪些行和列相关。

实现参考了fateice大神的代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000010;
int ans[maxn],x[maxn],y[maxn],f[maxn],mx[maxn];
struct node{
int x,y,val,pos;
bool operator <(const node& rhs)const{
return val<rhs.val;
}
};
node a[maxn];
int n,m;
int num(int i,int j){
return (i-1)*m+j;
}
inline int get(int x){
if(x==f[x])return x;
return f[x]=get(f[x]);
}
void merge(int x,int y){
f[get(x)]=get(y);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
int pos=num(i,j);
scanf("%d",&a[pos].val);
a[pos].x=i,a[pos].y=j,a[pos].pos=pos;
}
sort(a+1,a+1+n*m);
for(int i=1;i<=n+m;i++)
f[i]=i;
int i,j,k;
for(i=1;i<=n*m;i=j){
for(j=i;a[j].val==a[i].val&&j<=n*m;j++);
for(k=i;k<j;k++)merge(a[k].x,a[k].y+n);
for(k=i;k<j;k++){
int tmp=get(a[k].x);
mx[tmp]=max(mx[tmp],max(x[a[k].x],y[a[k].y]));
}
for(k=i;k<j;k++){
ans[a[k].pos]=mx[get(a[k].x)]+1;
x[a[k].x]=ans[a[k].pos];
y[a[k].y]=ans[a[k].pos];
}
for(k=i;k<j;k++){
mx[a[k].x]=0;
f[a[k].x]=a[k].x;
mx[a[k].y+n]=0;
f[a[k].y+n]=a[k].y+n;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
printf("%d ",ans[num(i,j)]);
}
printf("\n");
}
return 0;
}

  

Codeforces #345div1 C Table Compression (650C) 并查集的更多相关文章

  1. codeforces 651E E. Table Compression(贪心+并查集)

    题目链接: E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input s ...

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

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

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

  4. Codeforces 651E Table Compression【并查集】

    题目链接: http://codeforces.com/problemset/problem/650/C 题意: 给定n*m的矩阵,要求用最小的数表示每个元素,其中各行各列的大小关系保持不变. 分析: ...

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

    传送门 首先先从小到大排序,如果没有重复的元素,直接一个一个往上填即可,每一个数就等于当前行和列的最大值 + 1 如果某一行或列上有重复的元素,就用并查集把他们连起来,很(不)显然,处于同一行或列的相 ...

  6. Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心

    题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...

  7. Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分

    D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input: ...

  8. codeforces #541 D. Gourmet choice(拓扑+并查集)

    Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the wo ...

  9. Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序

    https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...

随机推荐

  1. Java源码阅读的真实体会(一种学习思路)【转】

    Java源码阅读的真实体会(一种学习思路)   刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动. 源码阅读,我觉得最核心有三点:技术基础+ ...

  2. phpstorm修改html模板

  3. brew安装php和扩展

    brew install homebrew/php/php56 --with-apache 报错: checking if the location of ZLIB install directory ...

  4. Equation

    You are given an equation: Ax2 + Bx + C = 0. Your task is to find the number of distinct roots of th ...

  5. 修改git commit 最后一次提交的注释信息 以及如何退出git bash vim编辑器

    https://www.cnblogs.com/sandy-happyhour/p/5950084.html 今天用git commit -m “注释”提交的时候,注释写错了,于是各种查资料开始了和g ...

  6. Windbg内核调试之二: 常用命令

    运用Windbg进行内核调试, 熟练的运用命令行是必不可少的技能. 但是面对众多繁琐的命令, 实在是不可能全部的了解和掌握. 而了解Kernel正是需要这些命令的指引, 不断深入理解其基本的内容. 下 ...

  7. Linux 终端 忽略大小写

    忘了在哪里看到的了,记录一下. 在-/.inputrc中加入一行 set completion-ignore-case on 搞定! 这样在终端输入.补全时就忽略大小写了.当然,Linux本身还是区分 ...

  8. mysql之 Innobackupex(全备+增量)备份恢复

    MySQL的热备(物理备份)可以采取全备加增量备份的方式来减轻数据库I/O压力及系统资源的占用.增量备份主要是以全备或增量备份为基础,备份那些变更过的页面.其备份的原理是基于一个不断增长的LSN序列, ...

  9. poj 1201 Intervals——差分约束裸题

    题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...

  10. poj 3421 X-factor Chains——质因数分解

    题目:http://poj.org/problem?id=3421 记忆化搜索竟然水过去了.仔细一想时间可能有点不对,但还是水过去了. #include<iostream> #includ ...