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. Docker的Mysql数据库:把数据存储在本地目录

    Docker mysql 把数据存储在本地目录,很简单,只需要映射本地目录到容器即可 1.加上-v参数 $ docker run -d -e MYSQL_ROOT_PASSWORD=admin --n ...

  2. PCIE_DMA实例四:xapp1052在Xilinx 7系列(KC705/VC709)FPGA上的移植

    PCIE_DMA实例四:xapp1052在Xilinx 7系列(KC705/VC709)FPGA上的移植 一:前言 这段时间有个朋友加微信请求帮忙调试一块PCIe采集卡.该采集卡使用xilinx xc ...

  3. 大数据入门第二十天——scala入门(二)scala基础01

    一.基础语法 1.变量类型 // 上表中列出的数据类型都是对象,也就是说scala没有java中的原生类型.在scala是可以对数字等基础类型调用方法的. 2.变量声明——能用val的尽量使用val! ...

  4. R绘图 第六篇:绘制线图(ggplot2)

    线图是由折线构成的图形,线图是把散点从左向右用直线连接起来而构成的图形,在以时间序列为x轴的线图中,可以看到数据增长的趋势. geom_line(mapping = NULL, data = NULL ...

  5. Webpack学习-Webpack初识

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

  6. 使用python处理百万条数据分享(适用于java新手)

    1.前言 因为负责基础服务,经常需要处理一些数据,但是大多时候采用awk以及java程序即可,但是这次突然有百万级数据需要处理,通过awk无法进行匹配,然后我又采用java来处理,文件一分为8同时开启 ...

  7. Linux Socket 编程简介

    在 TCP/IP 协议中,"IP地址 + TCP或UDP端口号" 可以唯一标识网络通讯中的一个进程,"IP地址+端口号" 就称为 socket.本文以一个简单的 ...

  8. unity物理检测的几种方式

    (由于本人大多做2d游戏,因此以下以2d为主介绍,但是具体和3d相差不大) 在unity中有很多不同的物理检测方式,但是大致可以分为以下几种: 1.Physics2d检测系列 Physics2d.Li ...

  9. unity上传app store遇到的一些问题

    记录ios发布时遇到的一些问题 注:如果你是用mac开发,那就在Unity里直接BuildAndRun就直接可以导到XCode里,如果是win,那就先打包成ios包,在传导Xcode里打开,不过可能会 ...

  10. EOS 权限管理之-权限的使用

    首先,跟大家说声抱歉,由于之前一直在准备EOS上线的一些工作,所以,很长时间没有更新内容.今天正好有时间,也想到了一些题材,就来说一下这个话题.本文完全是个人见解,如有不当之处,欢迎指出. 前提回顾: ...