Description

  

​   题目链接

  

  

  

Solution

  

​   显然每一列只能一起动,乱动则无解。

  

​   对原网格按列黑白染色,显然每一列数只能在相同颜色之间交换,乱动则无解。

  

​   之后考虑构造方案。

  

​   我们需要发(shou)现(wan)出一些好用的变换:

  

​   (1)使一种颜色的相邻两列同时上下翻转。

  

​   (2)使一种颜色的相邻两列交换,不翻转它们,而翻转另一个颜色中,不在这两列中间的,一个列。由于\(n \ge 5\),我们总能实现这个操作。

  

​   统计出黑色列总共需要使用(2)交换多少次达到目标、以及需要翻转多少次(由于使用(2)不影响上下状态,直接用初始状态作比较即可),记为\(flip_0\)与\(inv_0\),同理对白色列计算\(flip_1\)与\(inv_1\)。

  

​   我们首先使用(2)逐一实现每一个\(flip_0\),每做一次\(flip_0\)的同时,\(inv_1\)也会减少一次。同理每做一次\(flip_1\),\(inv_0\)也会减少一次。

  

​   优先做完\(flip_0\)与\(flip_1\),此时\(inv\)不论是正负都没关系(负也是有意义的),只要是偶数,就可以使用(1)逐个翻转回来。重要的是此时\(inv\)必须是偶数,这意味着没操作前,当且仅当\(flip_0\)与\(inv_1\)的奇偶性相同,且\(flip_1\)与\(inv_0\)的奇偶性相同时,才有解,否则无解。

  

​   对于\(flip\)的计算,使用冒泡排序类模拟的套路,通过统计原位置右边有多少已操作的数来计算出当前实际位置,从而确定每一次冒泡的步数。

  

​   总时间复杂度\(\mathcal O (n \log_2 n)\)

  

  

  

Code

    

#include <cstdio>
using namespace std;
const int N=100005;
int n;
int a[N][3],where[N];
int inv[2],flip[2];
inline int abs(int x){
return x<0?-x:x;
}
void readData(){
scanf("%d",&n);
for(int j=0;j<3;j++)
for(int i=1;i<=n;i++) scanf("%d",&a[i][j]);
}
bool preDraw(){
for(int i=1;i<=n;i++){
int id=(a[i][0]-1)/3+1;
if((i-id)&1) return false;
where[id]=i;
int x=(id-1)*3+1,y=x+1,z=y+1;
if(a[i][0]==x&&a[i][1]==y&&a[i][2]==z);
else if(a[i][0]==z&&a[i][1]==y&&a[i][2]==x)
inv[i&1]^=1;
else return false;
}
return true;
}
namespace BIT{
//suffix sum
int a[N];
inline void add(int u,int x){
for(;u;u-=u&-u)
a[u]+=x;
}
inline int que(int u){
int res=0;
for(;u&&u<=n;u+=u&-u)
res+=a[u];
return res;
}
inline void reset(){
for(int i=1;i<=n;i++)
a[i]=0;
}
}
void calcFlip(){
for(int i=1;i<=n;i+=2){
int nowpos=where[i]+BIT::que(where[i])*2;
flip[1]^=(abs(i-nowpos)/2)&1;
BIT::add(where[i],1);
}
BIT::reset();
for(int i=2;i<=n;i+=2){
int nowpos=where[i]+BIT::que(where[i])*2;
flip[0]^=(abs(i-nowpos)/2)&1;
BIT::add(where[i],1);
}
}
int main(){
readData();
if(!preDraw()){
puts("No");
return 0;
}
calcFlip();
if(inv[0]!=flip[1]||inv[1]!=flip[0])
puts("No");
else
puts("Yes");
return 0;
}

【AGC006E】 Rotate 3x3的更多相关文章

  1. 【leetcode】Rotate Image

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...

  2. 【leetcode】Rotate List(middle)

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  3. 【leetcode】Rotate Image(middle)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  4. 【LeetCode】Rotate Array

    Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...

  5. 【题解】【矩阵】【回溯】【Leetcode】Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  6. 【链表】Rotate List(三个指针)

    题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...

  7. 【数组】Rotate Image

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  8. 【Leetcode】【Medium】Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  9. 【Leetcode】【Medium】Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

随机推荐

  1. 20155202张旭 Exp3 免杀原理与实践

    20155202张旭 Exp3 免杀原理与实践 AV厂商检测恶意软件的方式主流的就三种: 基于特征码的检测 启发式恶意软件检测 基于行为的恶意软件检测 我们要做的就是让我们的恶意软件没法被这三种方式找 ...

  2. 20155338《网络对抗》Exp3 免杀原理与实践

    20155338<网络对抗>Exp3 免杀原理与实践 实验过程 一.免杀效果参考基准 Kali使用上次实验msfvenom产生后门的可执行文件,上传到老师提供的网址http://www.v ...

  3. [Oracle]OpenVMS 运行 Oracle 时的推荐值

    PQL Parameters            ORACLE Account  ------------------        -------------------------  PQL_M ...

  4. 汇编 if else

    知识点: if else 逆向还原代码 一.了解if else结构 sub esp, |. 8B45 FC MOV EAX,DWORD PTR SS:[EBP-] 0040102C |. 3B45 ...

  5. 校内模拟赛 Label

    题意: n个点m条边的无向图,有些点有权值,有些没有.边权都为正.给剩下的点标上数字,使得$\sum\limits_{(u,v)\in E}len(u,v) \times (w[u] - w[v]) ...

  6. springmvc 结合 自动封装异常信息输出为json 报错 500内部服务器错误的原因

    补充:还有一个原因是因为spring的对象没有被成功注入,例如 mapper没有被成功注入,抛出异常时在这种封装场景下将会抛出 500 服务器内部错误, 这种情况下要排查还是靠debug然后看看到底是 ...

  7. idea 项目java版本选项位置

    藏这里了 还有一个

  8. 微信小程序之授权 wx.authorize

    一. wx.authorize(Object object) 提前向用户发起授权请求.调用后会立刻弹窗询问用户是否同意授权小程序使用某项功能或获取用户的某些数据,但不会实际调用对应接口.如果用户之前已 ...

  9. KNN算法的R语言实现

    近邻分类 简言之,就是将未标记的案例归类为与它们最近相似的.带有标记的案例所在的类. 应用领域: 1.计算机视觉:包含字符和面部识别等 2.推荐系统:推荐受众喜欢电影.美食和娱乐等 3.基因工程:识别 ...

  10. SQL SERVER数据库升级手册

    背景 最近接手很多项目都跟数据库升级有关.感触还是颇深,写个心得,供大家参考,如果有疑问欢迎留言.     为什么升级?   你可能会因为各种各样的原因选择升级.我认为原因主要是3个方面 1.旧版本使 ...