CF650C Table Compression

给一个 \(n\times m\) 的非负整数矩阵 \(a\),让你求一个 \(n\times m\) 的非负整数矩阵 \(b\),满足以下条件

  1. 若 \(a_{i,j}<a_{i,k}\),则 \(b_{i,j}<b_{i,k}\)
  2. 若 \(a_{i,j}=a_{i,k}\),则 \(b_{i,j}=b_{i,k}\)
  3. 若 \(a_{i,j}<a_{k,j}\),则 \(b_{i,j}<b_{k,j}\)
  4. 若 \(a_{i,j}=a_{k,j}\),则 \(b_{i,j}=b_{k,j}\)
  5. \(b\) 中的最大值最小

\(n\times m\leq 10^6\)

建图+并查集


先考虑 \(a\) 中没有重复元素的情况

发现,我们只需要对于每行每列,按值域从小到大,相邻两位置连边,然后 \(b\) 每个位置的权值即为到最小数的距离,在 DAG 上遍历一遍即可

但是若 \(a\) 中有重复元素,直接建图就没有正确性了

\(trick\) :对于同一行同一列的重复元素,建立并查集,进行操作时只用对根节点进行操作

时间复杂度 \(O(nm\log nm)\)

代码

#include <bits/stdc++.h>
using namespace std;#define get(x, y) ((x - 1) * m + y)
typedef pair <int, int> pii;
const int maxn = 1e6 + 10;
int n, m, tot, a[maxn], f[maxn], par[maxn];
struct node {int x, y;bool operator < (const node& o) const {return a[get(x, y)] < a[get(o.x, o.y)];}
} dat[maxn];
vector <int> g[maxn];int find(int x) {return par[x] == x ? x : par[x] = find(par[x]);
}void unite(int x, int y) {par[find(x)] = find(y);
}int dfs(int u) {if (~f[u]) return f[u]; f[u] = 0;for (int v : g[u]) f[u] = max(f[u], dfs(v));return ++f[u];
}int main() {scanf("%d %d", &n, &m), tot = n * m;for (int i = 1; i <= tot; i++) {scanf("%d", a + i), par[i] = i;}for (int i = 1; i <= n; i++) {for (int j = 1; j <= m; j++) {dat[j] = node{i, j};}sort(dat + 1, dat + m + 1);for (int j = 1; j < m; j++) {int u = get(dat[j].x, dat[j].y);int v = get(dat[j + 1].x, dat[j + 1].y);if (a[u] == a[v]) unite(u, v);}}for (int j = 1; j <= m; j++) {for (int i = 1; i <= n; i++) {dat[i] = node{i, j};}sort(dat + 1, dat + n + 1);for (int i = 1; i < n; i++) {int u = get(dat[i].x, dat[i].y);int v = get(dat[i + 1].x, dat[i + 1].y);if (a[u] == a[v]) unite(u, v);}}for (int i = 1; i <= n; i++) {for (int j = 1; j <= m; j++) {dat[j] = node{i, j};}sort(dat + 1, dat + m + 1);for (int j = 1; j < m; j++) {int u = get(dat[j].x, dat[j].y);int v = get(dat[j + 1].x, dat[j + 1].y);if ((u = find(u)) != (v = find(v))) g[v].push_back(u);}}for (int j = 1; j <= m; j++) {for (int i = 1; i <= n; i++) {dat[i] = node{i, j};}sort(dat + 1, dat + n + 1);for (int i = 1; i < n; i++) {int u = get(dat[i].x, dat[i].y);int v = get(dat[i + 1].x, dat[i + 1].y);if ((u = find(u)) != (v = find(v))) g[v].push_back(u);}}memset(f, -1, sizeof f);for (int i = 1; i <= tot; i++) {if (find(i) == i) dfs(i);}for (int i = 1; i <= n; i++) {for (int j = 1; j <= m; j++) {printf("%d ", f[find(get(i, j))]);}putchar(10);}return 0;
}

一种 \(shortest\) 的做法

对于每个元素,按值域从小到大考虑,通过已访问到的行列最大值更新答案

时间复杂度 \(O(nm\log nm)\)

代码

#include <bits/stdc++.h>
using namespace std;#define get(x, y) ((x - 1) * m + y)
const int maxn = 1e6 + 10;
int n, m, tot, a[maxn], ans[maxn], par[maxn], val[2][maxn];
struct node {int x, y;bool operator < (const node& o) const {return a[get(x, y)] < a[get(o.x, o.y)];}
} dat[maxn];int find(int x) {return par[x] == x ? x : par[x] = find(par[x]);
}int main() {scanf("%d %d", &n, &m), tot = n * m;for (int i = 1; i <= n; i++) {for (int j = 1; j <= m; j++) {int pos = get(i, j);scanf("%d", a + pos), dat[pos] = node{i, j}, par[pos] = pos;}}sort(dat + 1, dat + tot + 1);for (int i = 1; i <= tot; i++) {int tx = dat[i].x, ty = dat[i].y, pos = get(tx, ty);int px = find(val[0][tx]), py = find(val[1][ty]), p = find(pos);ans[p] = max(ans[px] + (a[p] > a[px]), ans[py] + (a[p] > a[py]));if (a[p] == a[px]) par[px] = p;if (a[p] == a[py]) par[py] = p;val[0][tx] = val[1][ty] = p;}for (int i = 1; i <= n; i++) {for (int j = 1; j <= m; j++) {printf("%d ", ans[find(get(i, j))]);}putchar(10);}return 0;
}

转载于:https://www.cnblogs.com/Juanzhang/p/10424880.html

CF650C Table Compression相关推荐

  1. Table Compression Characteristics

    行存储压缩高级 : 使用或不使用直接路径或数组插入插入的行和更新的行使用高级行压缩进行压缩. About Table Compression

  2. php无重复字符的最长子串,PHP算法之无重复字符的最长子串

    给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc&qu ...

  3. Oracle 数据压缩(Compression) 技术 说明

    一.  官网说明 1.1 Oracle 11g Advanced Compression Oracle 11g EE版本中只有: Basic Table Compression ,而 AdvanceC ...

  4. Oracle 10g里面的table conpression

    Tom书里写到的: COMPRESS/NOCOMPRESS: Both ASSM and MSSM. Enables or disables compression of table data dur ...

  5. 一些mysql innodb的建议

    http://blog.csdn.net/yunhua_lee/article/details/8239145 原文:http://dev.mysql.com/doc/refman/5.5/en/in ...

  6. Innodb表压缩过程中遇到的坑(innodb_file_format) - billy鹏

    原文地址:http://www.cnblogs.com/billyxp/p/3342969.html 对于越来越多的数据,数据库的容量越来越大,压缩也就越来越常见了.在我的实际工作中进行过多次压缩工作 ...

  7. Oracle Schema Objects(Schema Object Storage And Type)

    One characteristic of an RDBMS is the independence of physical data storage from logical data struct ...

  8. An Implementation of Double-Array Trie

    An Implementation of Double-Array Trie Contents What is Trie? What Does It Take to Implement a Trie? ...

  9. Codeforces Round #345 (Div. 2)

    DFS A - Joysticks 嫌麻烦直接DFS暴搜吧,有坑点是当前电量<=1就不能再掉电,直接结束. #include <bits/stdc++.h>typedef long ...

最新文章

  1. 嵌入式系统学习笔记之五-- uboot常用命令 环境变量
  2. JAVA编程TXT文件_java开发之读写txt文件操作的实现
  3. [k8s] 第五章 Pod详解
  4. All about OpenGL ES 2.x – (part 2/3)(转载)
  5. JS中获取焦点和选中的元素
  6. C语言经典例39-在有序数组中插入一个数
  7. maven jdk 版本配置
  8. muduo之BlockingQueue
  9. 浏览器滚动条样式更改
  10. api pdo php,从PHP Mysql API转换为PDO时如何处理数据类型
  11. ISO9126软件质量模型
  12. Kafka 设计架构原理详细解析(超详细图解)
  13. 如何从iPhoto检索丢失的照片?
  14. 简单个人网页制作 大学生网页设计作业 静态HTML个人博客主页 DW个人网站模板下载 大学生简单个人网页作品代码
  15. 蓝宝石rx470d原版bios_AMD RX470/570强刷RX580完整图文教程(附文件下载及查BIOS攻略)...
  16. HP台式计算机不能启动,惠普电脑不能启动怎么处理
  17. 2021最新的NVIDIA显卡排行榜前十
  18. 第二次作业——时事点评
  19. 《留住好员工》-读后感
  20. html设置图片为黑白,CSS 将彩色图片转换成黑白图片

热门文章

  1. 51CTO博客弹出框精彩博文记录【2013年第二季度】
  2. NameNode任务线程之FSNamesystem$ReplicationMonitor
  3. C++ cctype定义的函数 - 学习笔记(7)
  4. 用DataBindings属性绑定控件的值
  5. SparkSQL核心编程
  6. PacBio三代测序
  7. Hibernate基本配置
  8. 2019.03.18 连接my sql
  9. 远震波形射线计算的局限性
  10. C语言:一个涉及指针函数返回值与printf乱码、内存堆栈的经典案例