CF237B Young Table 题解
CF237B Young Table 题解
题目描述
题目解法
题目中要求满足以下条件:
对所有的
对所有的
不难发现,对于样例:
4 3 5 6 1 2
将该表格进行排序后是满足条件的,如下:
1 2 3 4 5 6
又因为题目中给的是一个全排列,可以先记录好数字的位置然后再交换位置即可。
细节就看代码吧。
Code
#include<bits/stdc++.h> using namespace std; #define maxn 52 int len[maxn]; int mp[maxn][maxn]; void upd(int &x, int &y) // 表格中向后移动一步 { if(y==len[x]) y=1, x++; else y++; } pair<int, int> pos[maxn*maxn]; // 记录坐标 struct ans{int a, b, c, d;}; vector<ans> vc; // 记录答案 int main() { int n; cin>>n; for(int i=1;i<=n;i++) cin>>len[i]; for(int i=1;i<=n;i++) for(int j=1;j<=len[i];j++) cin>>mp[i][j], pos[mp[i][j]]={i, j}; int sum=accumulate(len+1, len+1+n, 0); // 求出数字个数 int x=0, y=0; for(int i=1;i<sum;i++) { upd(x, y); // 移动一步 if(mp[x][y]==i) continue; // 特判 vc.push_back({x,y,pos[i].first,pos[i].second}); int l=mp[x][y]; swap(mp[x][y], mp[pos[i].first][pos[i].second]); swap(pos[l], pos[i]); } cout<<vc.size()<<'\n'; for(auto [a, b, c, d]:vc) cout<<a<<' '<<b<<' '<<c<<' '<<d<<'\n'; // 输出答案 }
本文作者:Jimmy-LEEE
本文链接:https://www.cnblogs.com/redacted-area/p/18379522
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步