题意: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 (并查集)的更多相关文章

  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 650C Table Compression

    传送门 time limit per test 4 seconds memory limit per test 256 megabytes input standard input output st ...

  5. Code Forces 650 C Table Compression(并查集)

    C. Table Compression time limit per test4 seconds memory limit per test256 megabytes inputstandard i ...

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

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

  7. Codeforces Gym 100463E Spies 并查集

    Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...

  8. Codeforces 859E Desk Disorder 并查集找环,乘法原理

    题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...

  9. Codeforces - 828C String Reconstruction —— 并查集find()函数

    题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...

随机推荐

  1. 10.MVC框架开发(Ajax应用)

    1.MVC自带的Ajax应用, 使用步骤: 第一步,引入js框架 <script src="../../Scripts/jquery-1.4.4.js" type=" ...

  2. 开发设计模式(二) ActiveObject模式

    ActiveObject模式: ActiveObject模式和Command模式的配合使用是实现多线程控制的一项古老的技术,该模式有多种使用方式,为许多工业系统提供了一个简单的多任务核心. // 活动 ...

  3. 使用KVC

    KVC是Key Value Coding的简称,意思是键值编码,号称Cocoa的大招.它是一种可以直接通过字符串key(对象在名称)来访问或修改对象属性的机制. 使用 1.利用KVC可以随意修改一个对 ...

  4. C++ dll调用-动态(显式)

    C++ dll调用-动态(显式) 废话不说上代码, dll 头文件 j_test.h #pragma once extern "C"_declspec(dllexport) voi ...

  5. hbase 0.96 java 示例

    import java.util.ArrayList; import java.util.List; import org.apache.hadoop.conf.Configuration; impo ...

  6. CentOS查看CPU信息、位数、多核信息

    # uname -a Linux localhost.localdomain 2.6.18-164.el5PAE #1 SMP Thu Sep 3 04:10:44 EDT 2009 i686 i68 ...

  7. Winform datagridview相关操作

    datagridview显示行号的2种方法: 方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件在RowHeaderCell中绘制行号: privatevoiddat ...

  8. 中国海洋大学第四届朗讯杯高级组 I Cuckoo for Hashing

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2719&cid=1203 题意 :意思就是哈希来的,具体大意就是说有两个哈希表,然后有这样 ...

  9. 独立两套DJANGO+CELERY配置(生产+测试)时要注意的一些细节

    1,生产的NGINX环境,要指定自己的目录,而不是PROJ默认的. upstream ism_host { server ; } server { listen ; server_name local ...

  10. javaweb学习总结(三十七)——获得MySQL数据库自动生成的主键

    测试脚本如下: 1 create table test1 2 ( 3 id int primary key auto_increment, 4 name varchar(20) 5 ); 测试代码: ...