当前位置:网站首页>E. Cross Swapping (and check out deformation/good questions)
E. Cross Swapping (and check out deformation/good questions)
2022-08-10 15:05:00 【Luo gkv】
题意
给定一个n*n的二维矩阵.
执行操作:选择 1 < = k < = n , s w a p ( a [ i ] [ k ] , a [ k ] [ i ] ) , 1 < = i < = n 1<=k<=n,swap(a[i][k], a[k][i]),1<=i<=n 1<=k<=n,swap(a[i][k],a[k][i]),1<=i<=n
比如当n=4,k=3时,After conversion, it is shown on the right.

对于上述操作,We can do it any number of times,也可以是0次.
问,By doing the above several times,可以得到的字典序最小What is a two-dimensional array of .
定义二维数组aThe lexicographic order is :
将二维数组aMaps to a one-dimensional arrayb,映射规则 b[i*n+j] = a[i][j]
二维数组a1The lexicographical order is smaller than a two-dimensional arraya2,
Currently only if they are mappedb1 , b2满足
存在 i , b 1 [ j ] = = b 2 [ j ] , 1 < = j < i ; b 1 [ i ] < b 2 [ i ] i, b1[j] == b2[j], 1<=j<i; b1[i]<b2[i] i,b1[j]==b2[j],1<=j<i;b1[i]<b2[i]
1<=n<=1000
思路
上述操作,本质上就是将a[x][y]和a[y][x]做交换.
当前仅当k取x或y时,会影响到a[x][y]和a[y][x]是否交换.
k可以取1到n.操作k Essentially it's an exchangek行和第k列.
对于操作k,有两种选择,要么执行,要么不执行.
因此,我们发现:
- 当操作x和操作y 都执行,or when not executed,a[x][y]和a[y][x]没有做交换.
- 当操作x和操作y 一个执行,when one does not execute,a[x][y]和a[y][x]做了交换.
如下图,当前1,2After the operations are performed,a[1][2]和a[2][1]没有交换;而只执行1操作,a[1][2]和a[2][1]发生了交换.
After grasping the above rules,我们再研究,How to get the minimum lexicographical order.
由于是对称的,We only need to think about half of it,Here we consider all elementsa[i][j],下标i<=j的场景.
- 如果a[i][j] < a[i][j],Then do not exchange at this time,比如a[1][2] < a[2][1],This time after the exchange,The lexicographical order has instead increased.不交换,则操作iAnd the operation needs to be synchronized,要么都执行,要么都不执行.
- 如果a[i][j] > a[i][j],Then exchange at this time.交换,则操作iAnd the operation needs to be out of sync,一个执行,另一个不执行
- 此外,due to lexicographical order,is row-first,We want to row smaller elements,Swap is a priority/不交换.For example, we have studieda[1][2]Whether it needs to be exchanged after,再研究a[2][3]是否交换.因为a[1][2]ranking ratioa[2][3]高.
Get these rules,Have a related data structure come to mind–并查集.
- Treat each operation as a separate node,Initially the parent node is itself.
- The same number is required for the operation,We pull it into a collection,用union操作.
- For operations that require different signs,We also pulled it into a collection,But use positive and negative to distinguish them.
- Based on row precedence,next,We build all the operating edges one by one.
- 最终,Put the collection inside,为正的(负的)操作,去做执行,负的(正的)side does not execute.
详见代码
代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 1010;
int n;
int a[maxn][maxn];
int fa[maxn];
// 并查集初始化
void init() {
for (int i = 1; i <= n; ++i) {
fa[i] = i;
}
}
// 并查集查找根节点
int find1(int u) {
if (u < 0) {
// 负号 取反
return -find1(-u);
}
if (u == fa[u]) {
return u;
}
// Merge during query
return fa[u] = find1(fa[u]);
}
// 并查集 将u, vPull to the same collection 这里u, v可为负数
void union1(int u, int v) {
u = find1(u);
v = find1(v);
if (abs(u) == abs(v)) {
// already in the same collection
return;
}
if (u < 0) {
fa[-u] = -v;
} else {
fa[u] = v;
}
}
void solve() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
scanf("%d", &a[i][j]);
}
}
init();
for (int i = 1; i <= n; ++i) {
for (int j = i; j <= n; ++j) {
if (a[i][j] < a[j][i]) {
// 同号
union1(i, j);
} else if (a[i][j] > a[j][i]) {
// 异号
union1(i, -j);
}
}
}
for (int i = 1; i <= n; ++i) {
fa[i] = find1(i);
if (fa[i] > 0) {
// you can skip > 0, or skip < 0.
continue;
}
for (int j = 1; j <= n; ++j) {
swap(a[i][j], a[j][i]);
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j < n; ++j) {
printf("%d ", a[i][j]);
}
printf("%d\n", a[i][n]);
}
}
int main() {
int t;
// t = 1;
scanf("%d", &t);
while (t--) {
solve();
}
}
/* */
最后
I think the article is good,gongzhonghao 关注下 对方正在debug,一起快乐刷题吧~
边栏推荐
- 消息称原美图高管加盟蔚来手机 顶配产品或超7000元
- mysql进阶(三十三)MySQL数据表添加字段
- SWIG教程《四》-go语言的封装
- Based on Azuki Series: NFT Valuation Analysis Framework "DRIC"
- XML基本学习
- Boss raises salary!Look at my WPF Loading!!!
- Websocket realizes real-time change of chart content
- pm2 static file service
- High-paid programmers & interview questions series 135 How do you understand distributed?Do you know CAP theory?
- win2012安装Oraclerac失败
猜你喜欢

Using data intelligence, Amazon cloud technology helps companies build endogenous brand growth

Based on Azuki Series: NFT Valuation Analysis Framework "DRIC"

BCG库简介

MySQL Principle and Optimization: Update Optimization

产品使用说明书小程序开发制作说明

Rich Dad Poor Dad Reading Notes

12海里、24海里、200海里的意义及名称

Azure IoT Partner Technology Empowerment Workshop: IoT Dev Hack

波士顿房价预测

使用Uiautomator2进行APP自动化测试
随机推荐
基于inotify实现落盘文件的跨进程实时读写交互
C#实现访问OPC UA服务器
PAT甲级 1014 排队等候(队列大模拟+格式化时间)
sql语句 异常 Err] 1064 – You have an error in your SQL syntax; check the manual that corresponds to your
从洞察到决策,一文解读标签画像体系建设方法论
兆骑科创高层次人才创业大赛平台,投融资对接,双创服务
Second half of 2011 System Architect Afternoon Paper II
Azure IoT Partner Technology Empowerment Workshop: IoT Dev Hack
MQTT服务器搭建
Azure IoT 合作伙伴技术赋能工作坊:IoT Dev Hack
QOS功能介绍
mysql进阶(三十三)MySQL数据表添加字段
Do not access Object.prototype method ‘hasOwnProperty‘ from target object....
DB2查询2个时间段之间的所有月份,DB2查询2个时间段之间的所有日期
pytest框架优化
[Semantic Segmentation] DeepLab Series
基于 Azuki 系列:NFT估值分析框架“DRIC”
1004(树状数组+离线操作+离散化)
systemui状态栏添加新图标
Summary of tensorflow installation stepping on the pit