Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集
题目链接:
http://codeforces.com/problemset/problem/650/C
C. Table Compression
time limit per test4 secondsmemory limit per test256 megabytes
#### 问题描述
> Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many others. Inspired by the new knowledge, Petya is now developing the new compression algorithm which he wants to name dis.
>
> Petya decided to compress tables. He is given a table a consisting of n rows and m columns that is filled with positive integers. He wants to build the table a' consisting of positive integers such that the relative order of the elements in each row and each column remains the same. That is, if in some row i of the initial table ai, j
> Because large values require more space to store them, the maximum value in a' should be as small as possible.
>
> Petya is good in theory, however, he needs your help to implement the algorithm.
#### 输入
> The first line of the input contains two integers n and m (, the number of rows and the number of columns of the table respectively.
>
> Each of the following n rows contain m integers ai, j (1 ≤ ai, j ≤ 109) that are the values in the table.
#### 输出
> Output the compressed table in form of n lines each containing m integers.
>
> If there exist several answers such that the maximum number in the compressed table is minimum possible, you are allowed to output any of them.
####样例输入
> 2 2
> 1 2
> 3 4
样例输出
1 2
2 3
题意
给你一个n*m的方格,每个方格上有一个正整数,现在要把这些数离散化,要保证离散化之后同一行或同一列的数之间大小关系不变,构造一种解使得最大的那个值最小
题解
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=1e6+10;
int n,m;
int val[maxn],dp[maxn],ra[maxn],ind[maxn];
VI G[maxn];
VPII egs;
int fa[maxn];
int find(int x){
return fa[x]=fa[x]==x?x:find(fa[x]);
}
bool cmp(int a,int b){
return val[a]<val[b];
}
void init(){
for(int i=0;i<=n*m;i++) fa[i]=i,dp[i]=-1,ind[i]=0;
}
int main() {
scf("%d%d",&n,&m);
init();
rep(i,0,n) rep(j,0,m){
scf("%d",&val[i*m+j]);
}
//每行/每列 排序,然后连成串(这样可以少连很多没有用的边
rep(i,0,n){
rep(j,0,m) ra[j]=i*m+j;
sort(ra,ra+m,cmp);
for(int j=0;j<m-1;j++){
int p1=ra[j],p2=ra[j+1];
if(val[p1]<val[p2]){
egs.pb(mkp(p1,p2));
}else{
fa[find(p2)]=fa[find(p1)];
}
}
}
rep(j,0,m){
rep(i,0,n) ra[i]=i*m+j;
sort(ra,ra+n,cmp);
for(int i=0;i<n-1;i++){
int p1=ra[i],p2=ra[i+1];
if(val[p1]<val[p2]){
egs.pb(mkp(p1,p2));
}else{
fa[find(p2)]=fa[find(p1)];
}
}
}
//用并查集缩点
for(int i=0;i<egs.sz();i++){
int pu=find(egs[i].X),pv=find(egs[i].Y);
G[pu].pb(pv);
ind[pv]++;
}
//拓扑排序
queue<int> Q;
rep(i,0,n*m){
if(fa[i]!=i) continue;
if(ind[i]==0){
dp[i]=1;
Q.push(i);
}
}
while(!Q.empty()){
int u=Q.front(); Q.pop();
for(int i=0;i<G[u].sz();i++){
int v=G[u][i];
dp[v]=max(dp[v],dp[u]+1);
ind[v]--;
if(ind[v]==0) Q.push(v);
}
}
rep(i,0,n){
rep(j,0,m){
int x=dp[find(i*m+j)];
prf("%d",x);
if(j==m-1) puts("");
else prf(" ");
}
}
return 0;
}
//end-----------------------------------------------------------------------
Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集的更多相关文章
- codeforces Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集
C. Table Compression Little Petya is now fond of data compression algorithms. He has already studied ...
- Codeforces Round #345 (Div. 2) E. 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 ...
- 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 Round #346 (Div. 2) F. Polycarp and Hay 并查集 bfs
F. Polycarp and Hay 题目连接: http://www.codeforces.com/contest/659/problem/F Description The farmer Pol ...
- Codeforces Round #181 (Div. 2) B. Coach 带权并查集
B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...
- Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集
题目链接: 题目 F. Polycarp and Hay time limit per test: 4 seconds memory limit per test: 512 megabytes inp ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集
http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...
随机推荐
- 关于json时间数据格式转换与修改
使用easyui获取JSON时间数据时间数据时,通常是一长串的数字而不是我们想要的类似2018-11-01的普通时间格式. 此时我们就需要使用到关于JSON时间的格式化,而将时间转化成我们想要的格式. ...
- 如何使用yii2的缓存依赖特性
目录 如何使用yii2的缓存依赖特性 概述 页面缓存 缓存依赖 链式依赖 总结 如何使用yii2的缓存依赖特性 概述 缓存是Yii2的强大特性之一,合理使用缓存技术可以有效地减小服务器的访问压力.Yi ...
- 【二】调通单机版的thrift-C++版本
[任务2]调通单机版的thrift-C++版本 [任务2]调通单机版的thrift-C++版本 创建文件 安装boost开发工具 拷贝文件 [可忽略此步骤,如果c++代码直接编译无误的话] 编译 创建 ...
- Linux使用scp命令进行文件远程拷贝详解
前言 scp是 secure copy的缩写, scp是Linux系统下基于ssh登陆进行安全的远程文件拷贝命令.Linux的scp命令可以在Linux服务器之间复制文件和目录. 使用语法: scp ...
- ACM1019:Least Common Multiple
Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...
- Oracle——系统数据字典常用命令(查看表所属空间层目录等)
发生背景: 项目前后台交互对接时候,经常存在对底层表蒙圈情况尤其是oracle数据库,所在层级不同会导致操作对象直接的改变,从而发生意向不到的事情:很多时候需要了解我们所操作对象所处的层级等相关信息, ...
- Office 365部分安装及同时安装Visio的方法
From MWeb Win版本的Office 365安装包默认安装所有组件,没有选择的页面,在安装Office 365后再安装下载的Visio 2016专业版时,会显示计算机上已经安装了即插即用Off ...
- USB-Blaster驱动安装失败——文件哈希值不在指定目录中
右击此电脑,选择管理,选择设备管理器,更新USB-Blaster驱动出现问题 问题: 文件的哈希值不在指定的目录文件中,如图: 解决办法: Windows键+R→shutdown.exe /r /o ...
- SSM-CRUD入门项目——修改与PUT请求
修改 分析: 点击编辑,弹出用户修改的模态框, 模态框中显示用户的信息, 点击更新完成修改! 第一步先复制添加员工的模态框进行修改调整,完成修改员工的模态框的创建:(当然,相应的生成员工数 ...
- 20155211 《Java程序设计》实验四 Android程序设计
20155211 <Java程序设计>实验四 Android程序设计 一.实验内容及步骤 1.Android Stuidio的安装测试: 安装 Android Stuidio 完成Hell ...