[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堆内存泄露分析
一.背景 公司有一个中间的系统A可以对接多个后端业务系统B,一个业务系统以一个Namespace代表, Namespace中包含多个FrameChannel(用holder保存),表示A连接到业务 ...
- Java通过socket和DTU,RTU连接工业传感器通信
现在做DTU传感器监测数据一块,给大家分享如何通过socket技术连接到DTU,并能和DTU下面的传感器通信的,分享一下自己的心得和体会. 总体架构图 先来看下整体网络结构图. 工业名称解释 传感器: ...
- 题解 Wide Swap
题目传送门 题目大意 给出一个长度为 \(n\) 的排列 \(a_{1,2,...,n}\) 以及常数 \(k\),每次可以交换两个数 \(a_i,a_j\) 当且仅当 \(j-i\ge k \tex ...
- CF613D Kingdom and its Cities(虚树+贪心)
很休闲的一个题啊 其实一看到关于\(\sum k\)的限制,就知道是个虚树的题了 首先我们把虚树建出来,然后考虑怎么计算个数呢? 我们令\(f[x]\)表示以\(x\)的子树中,剩余了多少个还没有切断 ...
- JSR303数据校验
Springboot中可以用@validated来校验数据,如果数据异常则会统一抛出异常,方便异常中心统一处理. 导入依赖: <dependency> <groupId>org ...
- Java(46)类加载器
作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15201673.html 博客主页:https://www.cnblogs.com/testero ...
- 【MySQL】MySQL(四)存储引擎、索引、锁、集群
MySQL存储引擎 MySQL体系结构 体系结构的概念 任何一套系统当中,每个部件都能起到一定的作用! MySQL的体系结构 体系结构详解 客户端连接 支持接口:支持的客户端连接,例如C.Java.P ...
- 经典论文系列 | 缩小Anchor-based和Anchor-free检测之间差距的方法:自适应训练样本选择
前言 本文介绍一篇CVPR2020的论文,它在paperswithcode上获得了16887星,谷歌学术上有261的引用次数. 论文主要介绍了目标检测现有的研究进展.anchor-based和 ...
- JVM:内存结构
JVM:内存结构 说明:这是看了 bilibili 上 黑马程序员 的课程 JVM完整教程 后做的笔记 内容 程序计数器 虚拟机栈 本地方法栈 堆 方法区 直接内存 1. 程序计数器 1.1 定义 P ...
- 2021.8.14考试总结[NOIP模拟39]
T1 打地鼠 全场就俩人没切,还有一个是忘关$freopen$了. $code:$ 1 #include<bits/stdc++.h> 2 #define rin register sig ...