CF650C Table Compression
CF650C Table Compression
给一个 \(n\times m\) 的非负整数矩阵 \(a\),让你求一个 \(n\times m\) 的非负整数矩阵 \(b\),满足以下条件
- 若 \(a_{i,j}<a_{i,k}\),则 \(b_{i,j}<b_{i,k}\)
- 若 \(a_{i,j}=a_{i,k}\),则 \(b_{i,j}=b_{i,k}\)
- 若 \(a_{i,j}<a_{k,j}\),则 \(b_{i,j}<b_{k,j}\)
- 若 \(a_{i,j}=a_{k,j}\),则 \(b_{i,j}=b_{k,j}\)
- \(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相关推荐
- Table Compression Characteristics
行存储压缩高级 : 使用或不使用直接路径或数组插入插入的行和更新的行使用高级行压缩进行压缩. About Table Compression
- php无重复字符的最长子串,PHP算法之无重复字符的最长子串
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc&qu ...
- Oracle 数据压缩(Compression) 技术 说明
一. 官网说明 1.1 Oracle 11g Advanced Compression Oracle 11g EE版本中只有: Basic Table Compression ,而 AdvanceC ...
- Oracle 10g里面的table conpression
Tom书里写到的: COMPRESS/NOCOMPRESS: Both ASSM and MSSM. Enables or disables compression of table data dur ...
- 一些mysql innodb的建议
http://blog.csdn.net/yunhua_lee/article/details/8239145 原文:http://dev.mysql.com/doc/refman/5.5/en/in ...
- Innodb表压缩过程中遇到的坑(innodb_file_format) - billy鹏
原文地址:http://www.cnblogs.com/billyxp/p/3342969.html 对于越来越多的数据,数据库的容量越来越大,压缩也就越来越常见了.在我的实际工作中进行过多次压缩工作 ...
- Oracle Schema Objects(Schema Object Storage And Type)
One characteristic of an RDBMS is the independence of physical data storage from logical data struct ...
- An Implementation of Double-Array Trie
An Implementation of Double-Array Trie Contents What is Trie? What Does It Take to Implement a Trie? ...
- Codeforces Round #345 (Div. 2)
DFS A - Joysticks 嫌麻烦直接DFS暴搜吧,有坑点是当前电量<=1就不能再掉电,直接结束. #include <bits/stdc++.h>typedef long ...
最新文章
- 嵌入式系统学习笔记之五-- uboot常用命令 环境变量
- JAVA编程TXT文件_java开发之读写txt文件操作的实现
- [k8s] 第五章 Pod详解
- All about OpenGL ES 2.x – (part 2/3)(转载)
- JS中获取焦点和选中的元素
- C语言经典例39-在有序数组中插入一个数
- maven jdk 版本配置
- muduo之BlockingQueue
- 浏览器滚动条样式更改
- api pdo php,从PHP Mysql API转换为PDO时如何处理数据类型
- ISO9126软件质量模型
- Kafka 设计架构原理详细解析(超详细图解)
- 如何从iPhoto检索丢失的照片?
- 简单个人网页制作 大学生网页设计作业 静态HTML个人博客主页 DW个人网站模板下载 大学生简单个人网页作品代码
- 蓝宝石rx470d原版bios_AMD RX470/570强刷RX580完整图文教程(附文件下载及查BIOS攻略)...
- HP台式计算机不能启动,惠普电脑不能启动怎么处理
- 2021最新的NVIDIA显卡排行榜前十
- 第二次作业——时事点评
- 《留住好员工》-读后感
- html设置图片为黑白,CSS 将彩色图片转换成黑白图片