[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 ...
随机推荐
- Vue自定义标签页,并且在其中渲染Echarts图表
目录 一.需求说明 二.标签页功能实现 一.需求说明 1.点击标签按钮切换不同的echarts图表,考虑用Ant Design Vue,但是其样式无法自定义 2.div的整体布局样式使用tailwin ...
- SpringBoot下使用AspectJ(CTW)下不能注入SpringIOC容器中的Bean
SpringBoot下使用AspectJ(CTW)下不能注入SpringIOC容器中的Bean 在SpringBoot中开发AspectJ时,使用CTW的方式来织入代码,由于采用这种形式,切面Bean ...
- 题解 「2017 山东一轮集训 Day5」苹果树
题目传送门 题目大意 给出一个 \(n\) 个点的图,每个点都有一个权值 \(f_i\) ,如果 \(f_i=-1\) 表示 \(i\) 这个点是坏的.定义一个点是有用的当且仅当它不是坏的,并且它连的 ...
- FastAPI 学习之路(九)请求体有多个参数如何处理?
系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...
- C11 (GNU Dialect) -std=gnu11 和 -std=c11
C11 (GNU Dialect) -std=gnu11 和 -std=c11 C11 (GNU Dialect) -std=gnu11 和 -std=c11 用于 IntelliSense 的 C ...
- 八大排序算法之基数排序(python实现)
[写在前面] 参考文章: https://blog.csdn.net/nrsc272420199/article/details/82691596[给出的示例图,简单易懂,但是对于没一轮循环没有讲解的 ...
- 什么是Spring,SpringMVC,SpringBoot,SpringCloud?通俗易懂
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架.Spring使你能够编写更干净.更可管理.并且更易于测试的代码. Spring MVC是Spring的一个模块,一个web框 ...
- 深入理解Java虚拟机之类加载机制篇
概述 虚拟机把描述类的数据从 Class 文件加载到内存中,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,就是虚拟机的类加载机制. 在Java语言里面,类型的 ...
- 【UE4 设计模式】单例模式 Singleton Pattern
概述 描述 保证一个类只有一个实例 提供一个访问该实例的全局节点,可以视为一个全局变量 仅在首次请求单例对象时对其进行初始化. 套路 将默认构造函数设为私有, 防止其他对象使用单例类的 new运算符. ...
- 【数据结构与算法Python版学习笔记】引言
学习来源 北京大学-数据结构与算法Python版 目标 了解计算机科学.程序设计和问题解决的基本概念 计算机科学是对问题本身.问题的解决.以及问题求解过程中得出的解决方案的研究.面对一 个特定问题,计 ...