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;
}


【嵌入式DIY实例-Arduino篇】-脉搏传感器监测脉搏率 (BPM)及可视化 在本文中,我们将使用 Arduino 和脉搏传感器来测量心率 (BPM)。 在文章中,我们将向展示如何使用脉搏传感器测量您的心率(每分钟心跳次数),并在电脑上位机通过Processing来实现心率数据可视化。同时使用LCD16×2 显示屏显示脉搏率 BPM。 该传感器易于使用和操作。 只需将手指放在传感器顶部,它就会测量心跳变化。 阅读详情

相关推荐

Android Camera性能优化概述

做camera性能优化方面的工作有几年了,想写一篇文章简单介绍一下其中涉及到的具体技术,这篇文章会大概介绍一下涉及到的技术。

weixin_42136255的博客 4023

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

在ArcGIS Pro软件中进行滑坡的敏感性和易发性分析

滑坡的敏感性和易发性分析需要综合地形、水文、地质及环境等多维数据,本文罗列了基于ArcGIS Pro软件进行的一系列可执行分析。

weixin_49609713的博客 1732

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

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

Neutralzz的博客 397

STM32F103xx / STM32F429VET6最小系统原理图

心血来潮想试一下STM32F4系列和F1系列在编程上有什么差别,手头没有开发板,本着能勤俭节约的原则自己先做一块看看能不能成,看了一下STM32F429VET6的芯片在F429xx中最便宜,对照着数据手册绘制了原理图画了PCB,目前打板中,焊接完如果跑不通回来修改文章。这里我们可以看到,VDD是标准工作电压,VDDA是模拟工作电压,且VDDA必须与VDD等电位,VBAT是备份工作电压,一般接纽扣电池电源,如果存在VREF+引脚,则要求VDDA-VREF+ < 1.2V。截图下来比较糊,不知道为什么。

m0_51294753的博客 1万+

OBD诊断协议:OBDⅡ (SAE J1979) 和 OBDonUDS (SAE J1979-2) 和ZEV OBD技术架构的详细对比

摘要:文章对比了2025年最新OBD技术标准(SAEJ1979系列)的三大架构:传统OBD-II(已淘汰)、OBDonUDS(燃油/混动车)和ZEVOBD(新能源车)。核心发现:1)OBDonUDS与ZEVOBD共享UDS协议栈和DoIP通信标准,仅应用层数据(DID/DTC)不同;2)物理层统一为CAN+以太网,支持远程诊断和OTA;3)诊断工具需同时兼容J1979-2/3数据库。技术演进呈现"协议统一化、数据专用化"趋势,2027年后所有新车将强制采用新标准。

m0_56482679的博客 2390

Codeforces 650C Table Compression (并查集)

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

weixin_30616969的博客 164

一种导弹追踪算法的MATLAB仿真实现

以下是一个用于模拟机动变轨导弹和防空导弹追踪过程的 MATLAB 代码,同时会提供 3D 效果展示。你可以运行这段代码,从而观察机动变轨导弹和防空导弹的速度与位置变化。

SmartGridequation的博客 933

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

【异常】Claude Code CLI工具 模型加载异常 /init报错排查与完整解决方案 There‘s an issue with the selected model (glm-5). It

此类跨厂商模型配置报错,核心诱因是API生态的兼容性不匹配。优先使用工具原生支持的模型体系,可快速解决90%以上的同类报错;若需跨厂商使用目标模型,需完成API端点、鉴权密钥、模型ID的全链路适配,确保全流程配置合规,即可完成异常修复。

奶爸的编程之路,也就一周冷个三天 3704

9、硬件工程师笔试面试知识器件篇——三极管(2025版本)

本文全面介绍了三极管的基础知识与应用,包括其结构、类型、工作原理、工作状态、功能、应用场景及特性参数。详细探讨了三极管在信号放大、无线通信、音频设备及医疗设备中的具体应用,并深入分析了设计放大电路时如何平衡增益与噪声、优化偏置电路以提高稳定性、使用负反馈改善性能等关键问题。此外,文章还涵盖了高频电路设计、温度补偿策略、电磁干扰防护等实用内容,为硬件工程师提供了全面的技术参考和设计指导。

XU157303764的博客 7312

CodeForces 650C Table Compression

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

weixin_30677475的博客 110

opencode : 无法将“opencode“项识别为 cmdlet、函数、脚本文件或可运行程序的名称 解决方案

opencode命令识别问题与openclawcodexgemini确保的路径在 PATH 中。这个知识对所有 npm 全局工具通用。

一键难忘的博客 4521

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

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

BraketBN 770

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

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

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

weixin_30675967的博客 176

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

题解 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

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

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的博客 660

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 788

codeforces 650c

求最长路,相同的点用并查集缩点。。 不得不吐槽下这个博客,退出按键找了半天,重新登录这个号后,发表文章还是显示的那个号。本来写好了的题解,一下就没了,懒得重写了。#include #include #include #include #include using namespace std; #define maxn 1000005 int n,m; struct node { int

swust_lian的博客 477

Codeforces 651E:Table Compression

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

979
上一篇: .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、付费专栏及课程。

余额充值