Codeforces 650C Table Compression

Codeforces-Table:Codeforces竞赛表 Codeforces表,按国家/地区排序 关于 该项目是使用库的演示。 立即下载
C. Table Compression
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 < ai, k, then in the resulting table a'i, j < a'i, k, and if ai, j = ai, k then a'i, j = a'i, k. Similarly, if in some column j of the initial table ai, j < ap, j then in compressed table a'i, j < a'p, j and if ai, j = ap, j then a'i, j = a'p, 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.

Input

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

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.

Examples
input
2 2
1 2
3 4
output
1 2
2 3
input
4 3
20 10 30
50 40 30
50 60 70
90 80 70
output
2 1 3
5 4 3
5 6 7
9 8 7
Note

In the first sample test, despite the fact a1, 2 ≠ a21, they are not located in the same row or column so they may become equal after the compression.


题目还算好懂

M×N的矩阵 让你保持每行每列的大小对应关系不变,将矩阵重写,重写后的最大值最小。

当时看题,大小相等关系,一眼想到差分约束

于是建边,spfa tle on 15......

改用 dijkstra+heap tle on 66...真是66666

查看数据好像是网格图

每一层更新了好多好多次。。。简直丧心病狂!


比赛后看题解居然并查集。。当时完全没有想到

并查集的做法就是

合并每一行、每一列相等的元素

所有原值从小到大排序后,填空

填的时候 找到并查集中和他一个集合的所有元素

统计这些元素对应每一行每一列的最大值

然后填最大值+1就可以啦


其实还是就结了好长时间,因为感觉查询的时候总是n^2的复杂度

睡醒突然想到,不需要啊

只需要把他们都合并到一个根上,合并时把最大值更新在root上

这样只需要访问root就知道了集合对应的最大值


其中hx hy就是保存当前行列最大值对应的x,y

这样就可以按行列直接合并了

合并时把孩子的最大值统计到根上

查询时直接查询根的最大值+1就可以了

#include<bits/stdc++.h>
using namespace std;

const int lim=1e6+100;
struct self
{
    int x,y,w;
    self(){}
    self(int xx,int yy,int ww)
    {
        x=xx;
        y=yy;
        w=ww;
    }
}s[lim];

int cmp(self a,self b)
{
    return a.w<b.w;
}

int f[lim];
int t[lim];
int find(int i)
{
    return i==f[i]?i:f[i]=find(f[i]);
}

int ans[lim];
int maxx[lim],maxy[lim]; 

self hx[lim],hy[lim];

int m,n;

int h(int x,int y)
{
    return (x-1)*n+y;
}

int tot=0;
int x,y,w;

int main()
{
    scanf("%d%d",&m,&n);
    for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++)
        {
            scanf("%d",&w);
            tot++;
            s[tot]=self(i,j,w);
        }
    sort(s+1,s+tot+1,cmp);
    for(int i=1;i<=tot;i++)
        f[i] = i;

    int ll = 1;
    int rr;
    for(int k = 2; k <= tot+1; k++)
    {
        if(s[k].w != s[k-1].w || k == tot+1)
        {
            rr = k-1;
            for(int i = ll; i <= rr; i++)
            {
                int x = s[i].x;
                int y = s[i].y;
                int w = s[i].w;

                int r = find(h(x, y));
                if(hx[x].w != w)
                    hx[x] = s[i];
                else
                {
                    //cout<<" add "<<x<<","<<y<<"  to  "<<hx[x].x<<","<<hx[x].y<<endl;
                    int l = find(h(hx[x].x, hx[x].y));
                    if(l != r)
                    {
                        f[l] = r;
                        t[r] = max(t[r], t[l]);
                    }
                }
                if(hy[y].w != w)
                    hy[y] = s[i];
                else
                {
                    //cout<<" add "<<x<<","<<y<<"  to  "<<hy[y].x<<","<<hy[y].y<<endl;
                    int l = find(h(hy[y].x, hy[y].y));
                    if(l!=r)
                    {
                        f[l] = r;
                        t[r] = max(t[r], t[l]);
                    }
                }
                t[r] = max(t[r], maxx[x]);
                t[r] = max(t[r], maxy[y]);
                //cout<<"num "<<x<<","<<y<<" = "<<t[r]<<endl;
            }
            for(int i = ll; i <= rr; i++)
            {
                int num = h(s[i].x, s[i].y);
                int root = find(num);
                ans[num] = t[root]+1;
                maxx[s[i].x] = ans[num];
                maxy[s[i].y] = ans[num];
            }
            ll = k;
        }
    }
    for(int i = 1; i <= tot; i++)
    {
        printf("%d",ans[i]);
        if(i%n == 0)
            printf("\n");
        else
            printf(" ");
    }
    return 0;
}


Qt —— 百度地图 - 3、Qt调用高德在线地图功能示例(附源码) Qt - 地图相关 —— 3、Qt调用高德在线地图功能示例(附源码) 阅读详情

相关推荐

从TCGA官网获取新版count数据并精准转换为TPM的实战指南

本文详细介绍了从TCGA官网获取新版count数据并精准转换为TPM的实战指南。通过TCGAbiolinks工具下载原始count数据,结合GENCODE v36注释文件计算基因有效长度,最终实现count到TPM的标准化转换,确保数据完整性和分析可靠性。文章包含完整代码示例和常见问题解决方案,特别适合生物信息学研究人员参考。

weixin_27310417的博客 287

codeforces Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集

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 alg

c_czl的博客 280

高数 | 【极限与等价无穷小】两个重要极限 & 经典错误 & 什么情况下求极限可以直接带入

一. 第一个重要极限 二. 第二个重要极限 三. 导数定义式 全文见【高等数学】两个重要的极限 - 知乎

"You are worthy! You can do it!" 6万+

Codeforces 650C Table Compression (并查集+拓扑排序)

解析:不看tags都意识不到用图论。 一个位置对应一个节点,对于每行每列,值相同的用并查集union一起,值不相同的,值小的节点指向值大的节点。 压缩后节点的值是到该节点的最长的链的长度,可以通过拓扑排序求得。 [code]: #include #include #include #include using namespace std; const int maxn = 1e6

Neutralzz的博客 397

法律AI工具横评:Kimi Work与WorkBuddy,谁更适合法律人?

在人工智能技术日益普及的今天,自然语言处理(NLP)和大语言模型(LLM)正深刻改变着专业领域的工作范式。其核心原理在于通过海量数据训练,使模型能够理解、生成和推理人类语言。这一技术价值在于将从业者从繁琐、重复的信息处理工作中解放出来,极大提升工作效率与知识获取的便捷性。在法律、金融、咨询等对文本处理要求极高的行业,AI助手已展现出在文档分析、信息检索和内容生成方面的巨大潜力。具体到法律应用场景,AI工具能够辅助完成合同审查、法律检索和文书起草等核心任务,成为专业人士的得力助手。本文聚焦于近期备受关注的两款

weixin_30279671的博客 587

NVIDIA Isaac GR00T N1 人形机器人强化学习入门教程(二)详细步骤

LeRobot 格式 1.1 加载数据集 要加载数据集,我们需要定义以下 3 项内容: 在数据中显示图像帧 我们还可以对 LeRobotSingleDataset 类中的数据进行一系列转换。下面展示了如何对数据进行转换。 现在看看应用转换后的数据有何不同。 例如,对状态和操作进行规范化和串联,对视频图像进行裁剪、调整大小和颜色抖动。 二、GR00T 推断 本教程介绍如何使用 GR00

2506_90492529的博客 3016

Codeforces 650C Table Compression (并查集)

题意:M×N的矩阵 让你保持每行每列的大小对应关系不变,将矩阵重写,重写后的最大值最小。 思路:离散化思想+并查集,详见代码 好题! 1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 #include <algorithm> 5 #in...

weixin_30616969的博客 165

多尺度仿真软件:Copasi_(1).Copasi概述

Copasi(Complex Pathway Simulator)是一款开源的多尺度仿真软件,专为生物化学、系统生物学和合成生物学领域的研究人员设计。它不仅能够模拟细胞内和细胞间的生化反应网络,还能处理代谢途径、信号传导途径和基因调控网络等多种复杂系统。Copasi提供了用户友好的图形界面和强大的命令行接口,支持多种仿真算法和分析工具,使用户能够高效地进行模型构建、仿真和分析。Copasi允许用户创建自定义报告,以便生成特定格式的仿真结果。用户可以通过图形界面或命令行创建自定义报告。

kkchenjj的博客 865

Code Forces 650 C Table Compression(并查集)

C. Table Compression time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard output Little Petya is now fond of data compression algorithms. He...

weixin_30535565的博客 162

SAP S/4HANA Public Cloud 的 Development Tenant 深度解析

在传统的 SAP On-Premise 版本中,企业通常拥有完整的开发、测试、生产环境,开发人员可以直接访问 ABAP 代码,进行深度定制。然而,S/4HANA Public Cloud 采用了标准化的 SaaS 模型,不允许用户直接修改核心代码。因此,SAP 提供了一种新的开发方式,即 Development Tenant,允许企业在不破坏核心系统稳定性的前提下,实现个性化扩展。Development Tenant 主要用于 SAP S/4HANA Public Cloud 的扩展开发。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 853

PCL——KITTI数据集.bin文件转.pcd并可视化【2025最新版】

kitti数据集中的bin格式点云主要存储的字段为:`x`、`y`、`z`、`intensity`,对应于PCL中的PointXYZI。因此,只需将bin中的属性信息读取出来放入`PointXYZI`即可。本文给出简单易用的实现代码。博客长期更新,本文最近更新时间为:2025年1月11日。

点云侠的博客 3961

CodeForces 650C Table Compression

题目链接: http://codeforces.com/problemset/problem/650/C --------------------------------------------------------------------------------------------------------- 首先要知道的是这题原本相同的数最后是可以不同的 然后主要是要考虑到$Z$字形...

weixin_30677475的博客 110

【skill】漫剧 AI 视频创作的知识库 + API 调用手册

方案适用场景在对话中使用,自动触发注入到任何 LLM(Cursor/Windsurf/Claude API)Python 模块集成到自动化管线/批量生成GitHub 又限速了,不过前几轮搜到的已经够用了。根据搜索结果,结合已分析过的项目,给你整理完整对比 🐮。

突围 1060

【Codeforces650C】Table Compression【并查集】【拓扑排序】

先排序,把大小相同的元素合并成一个集合。注意这些元素不一定在位置上相邻。 然后再排序,从小到大建边。 最后做个bfs,每个点的答案为并查集中的最大值。 /* Footprints In The Blood Soaked Snow */ #include #include using namespace std; const int maxn = 1000005, maxq =

BraketBN 771

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

Codeforces #345div1 C Table Compression (650C) 并查集

题意:给你一个n*m的矩阵,需要在不改变每一行和每一列的大小关系的情况下压缩一个矩阵,压缩后的矩阵所有数的总和尽量的小。 思路:我们有这样的初步设想:对于在一行或一列的数x,y,若x<y,则建立一条x的位置到y的位置的边。之后进行拓扑排序的DP即可。然而会被卡边数卡掉,所以需要其它的解法。 新思路:我们把所有的数排个序,这样方便选对所有相同的数赋值。我们从小到大对所有的数赋值,合并这个数...

weixin_30675967的博客 177

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

题解 CF650C 【Table Compression】

题解 - CF650C\mathrm{CF650C}CF650C 题目意思 题目传送门 Sol\mathrm{Sol}Sol 并查集+拓扑 首先我们考虑关系图很能想到拓扑排序对吧!然后我们可以把一行中权值相同的点合成一个联通块。 于是就重新建图(及不同联通块之间连边) 还有我们不好找起点于是就随意定个虚点以其为起点跑有向图上的最长路即可。 时间复杂度:O(n×mlog⁡(n×m))O(n\times m \log (n\times m))O(n×mlog(n×m)) Code\m

wangyiyang2的博客 435

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Codeforces 651E Table Compression【并查集】

题目链接: http://codeforces.com/problemset/problem/650/C 题意: 给定n*m的矩阵,要求用最小的数表示每个元素,其中各行各列的大小关系保持不变。 分析: 将所有元素从小到大排序,然后找到每个元素相应位置进行填充,由于题目要求是每行每列的大小关系保持不变,所以填充的元素应为所在行和所在列中最大元素+1,保存好各行...

An55511的博客 153

Codeforces #345 div1 C. Table Compression 并查集 构造

题目 题目链接:http://codeforces.com/contest/650/problem/C 题目来源:http://codeforces.com/contest/650 简要题意:给定一个矩阵,令矩阵元素非负,最大元素最小,每行每列元素相对大小不变。

ned_chu的博客 663

Codeforces Round #345 (Div. 1) C. Table Compression (并查集)

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 alg

新月 1033

CF 651 E 并查集(矩阵关系不变) 挺好的

E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Little Petya is now fond of data compr

黑猫5027的博客 680

Codeforces Round #345 (Div. 1) C. Table Compression

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 alg

WZJRJ28 789
上一篇: .vimrc配置文件 + gvim 运行 gnome-terminal 完成后等待
下一篇: POJ 3169 Layout
abgnwl
博客等级 码龄12年 1粉丝 26原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值