[cf1458C]Latin Square
维护$n^{2}$个三元组$(x,y,z)$,每一个三元组描述$a_{x,y}=z$
对于RLDU这四个操作,即将所有三元组的$x$或$y$执行$\pm 1$(模$n$意义下)
对于IC这两个操作,即分别将$y$和$z$交换或$x$和$z$交换
维护这三个元素交换的顺序以及每一个元素加减的量,最终再$o(n^{2})$解出即可
时间复杂度为$o(n^{2}+m)$,可以通过


- 1 #include<bits/stdc++.h>
- 2 using namespace std;
- 3 #define N 1005
- 4 #define M 100005
- 5 int t,n,m,num[11],p[3],d[3],b[3],a[N][N],ans[N][N];
- 6 char s[M];
- 7 int read(){
- 8 int x=0;
- 9 char c=getchar();
- 10 while ((c<'0')||(c>'9'))c=getchar();
- 11 while ((c>='0')&&(c<='9')){
- 12 x=x*10+c-'0';
- 13 c=getchar();
- 14 }
- 15 return x;
- 16 }
- 17 void write(int x,char c='\0'){
- 18 while (x){
- 19 num[++num[0]]=x%10;
- 20 x/=10;
- 21 }
- 22 if (!num[0])putchar('0');
- 23 while (num[0])putchar(num[num[0]--]+'0');
- 24 putchar(c);
- 25 }
- 26 int main(){
- 27 t=read();
- 28 while (t--){
- 29 n=read(),m=read();
- 30 for(int i=1;i<=n;i++)
- 31 for(int j=1;j<=n;j++)a[i][j]=read();
- 32 scanf("%s",s);
- 33 //当前第i维为原来第pi维+di
- 34 p[0]=0,p[1]=1,p[2]=2,d[0]=d[1]=d[2]=0;
- 35 for(int i=0;i<m;i++){
- 36 if (s[i]=='R')d[1]++;
- 37 if (s[i]=='L')d[1]--;
- 38 if (s[i]=='D')d[0]++;
- 39 if (s[i]=='U')d[0]--;
- 40 if (s[i]=='I'){
- 41 swap(p[1],p[2]);
- 42 swap(d[1],d[2]);
- 43 }
- 44 if (s[i]=='C'){
- 45 swap(p[0],p[2]);
- 46 swap(d[0],d[2]);
- 47 }
- 48 }
- 49 for(int i=1;i<=n;i++)
- 50 for(int j=1;j<=n;j++){
- 51 b[0]=i,b[1]=j,b[2]=a[i][j];
- 52 int x=((b[p[0]]+d[0])%n+n-1)%n+1;
- 53 int y=((b[p[1]]+d[1])%n+n-1)%n+1;
- 54 int z=((b[p[2]]+d[2])%n+n-1)%n+1;
- 55 ans[x][y]=z;
- 56 }
- 57 for(int i=1;i<=n;i++){
- 58 for(int j=1;j<n;j++)write(ans[i][j],' ');
- 59 write(ans[i][n],'\n');
- 60 }
- 61 }
- 62 return 0;
- 63 }
[cf1458C]Latin Square的更多相关文章
- 算法:拉丁方阵(Latin Square)
拉丁方阵(英语:Latin square)是一种 n × n 的方阵,在这种 n × n 的方阵里,恰有 n 种不同的元素,每一种不同的元素在同一行或同一列里只出现一次.以下是两个拉丁方阵举例: 拉丁 ...
- namespace_std 杂题选讲
CF1458C Latin Square 2021 EC Final C. Random Shuffle [THUPC2021] 混乱邪恶 [JOISC2022] 制作团子 3 2022 集训队互测 ...
- USACO 6.5 All Latin Squares
All Latin Squares A square arrangement of numbers 1 2 3 4 5 2 1 4 5 3 3 4 5 1 2 4 5 2 3 1 5 3 1 2 4 ...
- 解数独(Python)
0.目录 1.介绍 2.一些通用函数 3.全局变量(宏变量) 4.数独预处理(约束传播) 5.解数独(深度优先搜索+最小代价优先) 6.主函数 7.总代码 1.介绍 数独是一个非常有趣味性的智力游戏, ...
- [LeetCode] Matchsticks to Square 火柴棍组成正方形
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- [LeetCode] Valid Word Square 验证单词平方
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- [LeetCode] Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- [LeetCode] Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- OPEN CASCADE Gauss Least Square
OPEN CASCADE Gauss Least Square eryar@163.com Abstract. The least square can be used to solve a set ...
随机推荐
- JVM学习笔记——类加载器与类加载过程
类加载器与类加载过程 类加载器ClassLoader 类加载器 ClassLoader 用于把 class 文件装载进内存. 启动类加载器(Bootstrap ClassLoader): 这个类加载使 ...
- ansible远程运维操作
1.command 用于查看文件内容,查看磁盘,内存,启动命令等纯命令信息 ansible portal -m command -a "cat /test1/test"2.ping ...
- RabbitMQ的web页面介绍(三)
一.Virtual Hosts 每一个 RabbitMQ 服务器都能创建虚拟的消息服务器,我们称之为虚拟主机 (virtual host) ,简称为vhost.每一个 vhost 本质上是一个独立的小 ...
- Java语言程序设计与数据结构(基础篇)第七章答案
答案为本人求解,如有错误,还望海涵.如有雷同,纯属巧合. 7.1 import java.util.Scanner; public class Main { public static void ma ...
- MySQL中如何选择合适的备份策略和备份工具
数据库备份的重要性毋庸置疑,可以说,它是数据安全的最后一道防线.鉴于此,对于备份,我们通常会做以下要求: 多地部署 对于核心数据库,我们通常有两地三中心的部署要求.对于备份来说,也是如此. 一个备份 ...
- allure报告中allure.title 如何去掉后方的参数化显示
1.解决方法如下 listener.py 文件位置:Lib\site-packages\allure_pytest\listener.py (第三方包所在的LIb目录) 将下图中红色部分test_re ...
- 利用 CSS Overview 面板重构优化你的网站
本文将向大家介绍 Chrome 87 开始支持的 CSS Overview Panel,并且介绍如何更好地利用这个面板.通过 CSS Overview Panel,可能可以帮助我们: 更准确(高保真) ...
- pycharm环境下配置scrap爬虫环境
[写在开头] 参考文章后面给出了备注信息,是在解决这个问题的时候,查找的比较有亮点的参考文章,如果本文章写的不太清楚的,可以去原文章进行查看.下面列举的四个文章有参考的成分也有验证的成分,解决办法重点 ...
- find+xargs+sed批量替换
写代码时经常遇到要把 .c 和 .h的文件中的某些内容全部替换的情况,用sourceinsight 进行全局的查找是一个方法,但是sourceinsight只能替换一个文件中的字符串,不能同时替换多 ...
- 如果你还不知道Apache Zookeeper?你凭什么拿大厂Offer!!
很多同学或多或少都用到了Zookeeper,并知道它能实现两个功能 配置中心,实现表分片规则的统一配置管理 注册中心,实现sharding-proxy节点的服务地址注册 那么Zookeeper到底是什 ...