Codeforces 650C Table Compression (并查集)
题意:M×N的矩阵 让你保持每行每列的大小对应关系不变,将矩阵重写,重写后的最大值最小。
思路:离散化思想+并查集,详见代码
好题!
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <bits/stdc++.h>
using namespace std;
#define clc(a,b) memset(a,b,sizeof(a))
const double pi=acos(-);
const int maxn=;
int k,n,m,tot,i,j;
struct data {
int r,c,v,id;
} p[maxn];
bool cmp(const data &a,const data &b) {
return a.v<b.v;
}
int f[maxn],X[maxn],Y[maxn],x[maxn],y[maxn],ans[maxn],tmp[maxn]; int F(int a) {
return a==f[a]?f[a]:f[a]=F(f[a]);
} int main() {
//freopen("in.txt","r",stdin);
scanf("%d%d",&n,&m);
tot=;
clc(tmp,);
clc(f,-);
for( i=; i<=n; i++) {
for( j=; j<=m; j++) {
scanf("%d",&p[++tot].v);
p[tot].r=i,p[tot].c=j;
p[tot].id=tot;
f[tot]=tot;
}
}
sort(p+,p++tot,cmp);
for(i=; i<=tot; i=j) {
for(j=i; p[i].v==p[j].v; j++);//每次只处理相同元素
for(k=i; k<j; k++) {//每行每列并查集统计,归并到一个集合
int r=p[k].r,c=p[k].c;
if(!x[r]) x[r]=k;
else
f[F(k)]=F(x[r]);
if(!y[c]) y[c]=k;
else
f[F(k)]=F(y[c]);
}
for(k=i; k<j; k++) {//当前值的改变值 应该是该元素所在行或列 前一个填入的值再加一
int q=F(k);
tmp[q]=max(tmp[q],max(X[p[k].r],Y[p[k].c])+);
}
for(k=i; k<j; k++) {//X Y数组维护行和列的最大填入值
x[p[k].r]=y[p[k].c]=;
X[p[k].r]=Y[p[k].c]=ans[p[k].id]=tmp[F(k)];
}
}
for(i=; i<=tot; i++) {
if(i%m==)
printf("%d",ans[i]);
else
printf(" %d",ans[i]);
if(i%m==)
printf("\n");
}
return ;
}
Codeforces 650C 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 650C Table Compression
传送门 time limit per test 4 seconds memory limit per test 256 megabytes input standard input output st ...
- Code Forces 650 C Table Compression(并查集)
C. Table Compression time limit per test4 seconds memory limit per test256 megabytes inputstandard i ...
- Codeforces 651E Table Compression【并查集】
题目链接: http://codeforces.com/problemset/problem/650/C 题意: 给定n*m的矩阵,要求用最小的数表示每个元素,其中各行各列的大小关系保持不变. 分析: ...
- 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 ...
随机推荐
- OpenFileDialog组件打开文件....待续
1.常用属性 InitialDirectory 对话框的初始目录 this.openFileDialog1.InitialDirectory = "d:\\"; ...
- Razor语法小记
1.代码块中,<text>标签用来输出,如: @{ <text>sdfsdf</text> } 输出Html: sdfsdf
- Nhibernate cookbook 3.0-翻译
/* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-ts ...
- delphi xe5 android 开发数据访问手机端 解决乱码的办法
经过测试,将sqlserver里的字段由varchar 或者char 改为 nvarchar 或者nchar 然后在手机端的clientdataset 增加字段的时候数据类型选择widestrin ...
- Dynamic Programming (DP) 问题总结
所有的 DP 问题都可以简单得用 Recursion 的方式实现.这通常是最容易想到的思路. 问题是这种实现不够 efficient,存在 subproblem 被重复计算的情况.有两种解决这个问题的 ...
- 教你6步定制你的Ubuntu桌面
转自教你6步定制你的Ubuntu桌面 对于那些想要一个易于使用的界面的用户,Ubuntu是一个很好的Linux发行版,并且对于一个Linux新手也可以说是最好的Linux发行版.不过这产生了一些副作用 ...
- POJ2301+水~~~~~~
有比这更水的么.............. #include<stdio.h> int main(){ int n; scanf("%d",&n); while ...
- HDU 5015 233 Matrix
题意:给定一个矩阵的第0列的第1到n个数,第一行第1个数开始每个数分别为233, 2333........,求第n行的第m个数. 分析: 其实也没那么难,自己想了半天还没往对的方向想,m最大1e9,应 ...
- c helloworld on zynq
A linux os runs on zynq board. I want ro run a hello world c program on it. I linked zynq board to a ...
- 李洪强漫谈iOS开发[C语言-016]-变量的作用域