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. 20155209林虹宇逆向及Bof基础实验报告

    20155209林虹宇逆向及Bof基础实验报告 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符 ...

  2. WPF应用

    代码 private void button1_Click(object sender, RoutedEventArgs e) { calculate sa = new calculate(int.P ...

  3. Vue 使用细节收集

    JSX 中 on 开头的属性名 在用 elementui 中的 el-upload 的时候,他们组件中有一个属性 on-change ,也不知道谁想出来的属性名,太扯淡了,非要 on 开头,我开始的代 ...

  4. python 字符串的split()函数详解

    split翻译为分裂.  split()就是将一个字符串分裂成多个字符串组成的列表. split()当不带参数时以空格进行分割,当代参数时,以该参数进行分割. //---当不带参数时 example: ...

  5. RegExp,实现匹配合法时间(24小时制)的正则表达式

    合法时间格式  00:00:00 - 23:59:59   格式分析:H + ":" + M + ":" + S   H-分析: 00:00:00 - 09:5 ...

  6. Webpack学习-Webpack初识

    一.前言 webpack 到底是个什么东西呢,看了一大堆的文档,没一个能看懂的,因为上来就是给个module.exports 然后列一大堆配置,这个干啥,那个干啥,没一点用.但凡要用一个东西,一个东西 ...

  7. 【ORACLE】Win2008R2修改oracle数据库实例名

    需求说明:要求将windows平台的数据库实例名由orcl改为haha 参考: https://www.cnblogs.com/junnor/archive/2013/03/05/2945245.ht ...

  8. docker之容器数据持久化

    1.挂载本地目录为容器的数据存放目录 [root@node03 ~]# docker run -itd --name web01 -v /container_data/web:/data ubuntu ...

  9. 机器视觉及图像处理系列之二(C++,VS2015)——图像级的人脸识别(1)

    接上一篇,一切顺利的话,你从github上clone下来的整个工程应该已经成功编译并生成dll和exe文件了:同时,ImageMagic程序亦能够打开并编辑图像了,如此,证明接下来的操练你不会有任何障 ...

  10. 机器视觉及图像处理系列之一(C++,VS2015)——搭建基本环境

    自<人脸识别>系列发布至今,已一年多矣,期间除答复些许同好者留言外,未再更新文,盖因项目所迫,不得已转战它途,无暇.无料更博耳.其时,虽人已入项目中,然终耿怀于人脸识别方案之谬.初,写此文 ...