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 ...
随机推荐
- NFS挂载及写入故障
最近在做架构时,分离出来一台图片服务器,图片服务器是通过NFS(网络文件系统)给两台web服务器提供图片存储的,在编辑NFS配置文件(/etc/exports)时:想了一下,允许访问NFS共享目录的范 ...
- hdu 4358 Boring counting 离散化+dfs序+莫队算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意:以1为根节点含有N(N <= 1e5)个结点的树,每个节点有一个权值(weight ...
- Sublime Text 3插件之Emmet:HTML/CSS代码快速编写神器
一.快速编写HTML代码 1. 初始化 HTML文档需要包含一些固定的标签,比如<html>.<head>.<body>等,现在你只需要1秒钟就可以输入这些标签. ...
- python 内建函数 str() 和 repr() 的区别
1.内建函数str()和repr() 或反引号操作符(``)可以方便地以字符串的方式获取对象的内容.类型.数值属性等信息. 2.str()函数得到的字符串可读性好(故被print调用) 3.repr( ...
- jersey post提交到 ContainerRequestFilter 而HttpServletRequest获取不到数据(转)
jersey post提交到 ContainerRequestFilter 而HttpServletRequest获取不到数据 问题:在serverfilter request获取不到post提交的 ...
- opencv 操作本地摄像头实现录像
直接上代码: // demo1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> usin ...
- 【弱省胡策】Round #6 String 解题报告
感觉这个题好神啊. 首先我们只管 $a = b$ 的情况,那么我们自然就可以把这个串对 $a$ 取模,然后用 KMP 求出能弄出几个其他的 B 串. 具体就是把串先倍长,然后倒过来,然后求 $Next ...
- [转载]如何打一手好Log
如果项目上过线的话,那你一定知道Log是多么重要. 为什么说Log重要呢?因为上线项目不允许你调试,你只能通过Log来分析问题.这时打一手好Log的重要性绝不亚于写一手好代码.项目出问题时,你要能拿出 ...
- 使用JProfiler进行内存分析
在最近的工作中,通过JProfiler解决了一个内存泄漏的问题,现将检测的步骤和一些分析记录下来,已备今后遇到相似问题时可以作为参考. 运行环境: Tomcat6,jdk6,JProfiler8 内存 ...
- FFT初步学习小结
FFT其实没什么需要特别了解的,了解下原理,(特别推荐算法导论上面的讲解),模板理解就行了.重在运用吧. 处理过程中要特别注意精度. 先上个练习的地址吧: http://vjudge.net/vjud ...