题意: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之图形处理

    图形处理 PHP 的图形处理,主要功能集中在 PHP 的图形处理函数. 需要先掌握一些要点.什么叫图片,怎么显示图片. 所谓的图片,其实也是一种文件,只是内容不是我们肉眼直接可见的.如果我们用记事本打 ...

  2. PHP学习心得(八)——运算符

    运算符是可以通过给出的一或多个值(用编程行话来说,表达式)来产生另一个值(因而整个结构成为一个表达式)的东西.所以可以认为函数或任何会返回一个值(例如 print)的结构是运算符,而那些没有返回值的( ...

  3. bottle + vue.js 打造你的单页应用

    看到各种单页应用之后,觉得单页应用简直酷毙了,作为一个技术宅,在假期的 时候恶补了一下vue ,觉得还不错,不过想想前端的东西毕竟还是太广博了.我也不知道怎么写反正应用起来有点别扭,就是资料太少了,成 ...

  4. Be Pythonic ,Google Python Style Guide

    为了更规范的写代码,变得更专业 分号 1 不在句末添加分号,不用分号在一行写两句代码 行长度 2 每行不超过80字符,python会隐式行连接圆括号,中括号,花括号中的字符,如多参数方法调用可以写为多 ...

  5. SSH搭建完美CURD,含分页算法

    今日开始研究使用java平台上的框架解决web服务端的开发. 这是一个完整的SSH实例,在马士兵老师的SSH整合代码基础上,增加用户的增删改查,同时实现structs方式的分页 放出源代码供大家学习参 ...

  6. java JDBC操作MySQL数据库

    一,首先在MYSQL建立一个数据库,例如Geek99DB: create database Geek99DB; use Geek99DB; 然后建立一个表CustomerTab: create tab ...

  7. C语言字符知识狭区

    C语言字符在用户接口软件编程上经常用到,但是有一些狭区会让编程出现一些小BUG,现在总结与此. 1.'\\' 代表的是字符\,而'\'是不能代表字符\的.通常\后面都要跟上数字或者其他字母来表示一个特 ...

  8. 洛谷1508 Likecloud-吃、吃、吃

    题目背景 问世间,青春期为何物? 答曰:“甲亢,甲亢,再甲亢:挨饿,挨饿,再挨饿!” 题目描述 正处在某一特定时期之中的李大水牛由于消化系统比较发达,最近一直处在饥饿的状态中.某日上课,正当他饿得头昏 ...

  9. iOS最新上线流程+续费 2015-7-20更新

    一.程序上线前准备 确认图标是否⻬全,应⽤的icon图标 在以前图⽚片直接命名 为icon就可以了,在xcode5以后,苹果加 ⼊入了images.xcasset这个⽂文件夹,所有的 图标全都在这⾥里 ...

  10. python 程序列表

    用 python  通过读取注册表来获取机器安装的程序列表,包括,软件名称,版本号,安装日期等 # -*- coding: UTF8 -*-import _winregimport osimport ...