题意: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. PHP初学留神(五)·小结

    来学习快两个月了,这周末即将回家开始写论文.那么走之前,好好总结一下这两个月的所学所得吧.这段时间,在实验室里做的Web开发主要涉及到了web开发的一些框架内容以及php基础知识.思维导图记录如下. ...

  2. sencha architect/sencha touch , to prevent breakpoint lost when you debug

    add this to your loader config: Ext.Loader.setConfig({ disableCaching: false }); or : click 'applica ...

  3. AJAX技术的核心

    //创建一个XMLHttpRequest对象 ,利用此对象与服务器进行通信 是AJAX技术的核心 /** * 获取XmlHttpRequest对象 */ function getXMLHttpRequ ...

  4. [CC150] Get all permutations of a string

    Problem: Compute all permutations of a string of unique characters. 此题用循环的方式不好做,下面是一种递归的思路: 把给的字符串看成 ...

  5. Spatial Pyramid Matching 小结

    Spatial Pyramid Matching 小结 稀疏编码系列: (一)----Spatial Pyramid 小结 (二)----图像的稀疏表示——ScSPM和LLC的总结 (三)----理解 ...

  6. 机器学习算法与Python实践之(四)支持向量机(SVM)实现

    机器学习算法与Python实践之(四)支持向量机(SVM)实现 机器学习算法与Python实践之(四)支持向量机(SVM)实现 zouxy09@qq.com http://blog.csdn.net/ ...

  7. Weex详解:灵活的移动端高性能动态化方案

    原文地址:http://www.infoq.com/cn/articles/introducing-weex 在2016年4月份的QCon上,阿里巴巴资深总监,淘宝移动平台及新业务事业部.阿里百川负责 ...

  8. SDUT 1269 走迷宫(BFS)

    点我看题目 题意:中文不详述. 思路 :上上上场比赛让一个BFS给虐了,上次比赛让一个三维的给废掉了.......所以急于从水题刷起......还因为数组开小了WA了5,6次 #include < ...

  9. POJ2221+模拟

    参考http://blog.sina.com.cn/s/blog_7de5c6210100tm1h.html 其实是水题............ #include<string.h> #i ...

  10. [Gauss]POJ1753 Flip Game

    题意:给4×4的棋盘的初始状态,b代表黑,w代表白. 要求变成全黑或者全白 最少需要几步. 简单的做法 可以暴搜 状压bfs 不再赘述 主要学习Gauss做法 同样是01方程组 用异或解 注意全黑或全 ...