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. C/C++函数调用方式

    __cdecl __fastcall与__stdcall,三者都是调用约定(Calling convention),它决定以下内容:1)函数参数的压栈顺序,2)由调用者还是被调用者把参数弹出栈,3)以 ...

  2. 11.10 (上午)开课二个月零六天(ajax基础,ajax做登录)

    test.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  3. Hadoop开发第3期---Hadoop的伪分布式安装

    一.准备工作 1. 远程连接工具的安装 PieTTY 是在PuTTY 基础上开发的,改进了Putty 的用户界面,提供了多语种支持.Putty 作为远程连接linux 的工具,支持SSH 和telne ...

  4. effective c++ 笔记 (45-48)

    //#45   运用成员函数模版接受所有兼容类型 { /*  1:当你使用智能指针的时候,会发生一个问题,想把一个子类的对象赋给基类的指针变得不可能了, 因为智能指针指定了的是基类的类型,而赋给它的是 ...

  5. gulp:入门简介

    本文是gulp的入门级介绍,主要内容包括什么是gulp,gulp与grunt有什么区别,gulp可以解决grunt存在的哪些问题,以及一个简单的说明例子. 什么是gulp gulp的官方定义非常简洁: ...

  6. JMeter:生成漂亮的接口/压力测试的HTML报告

    JMeter生成HTML网页报告(非gui模式操作) 我们做性能测试的时候会经常使用一些性能测试工具,比如loardrunner和jmeter,我个人比较喜欢Jmeter这个工具,jmeter之前版本 ...

  7. 如何在百度云虚拟机中配置thinkphp5,并且url去掉index.php

    第一步:将public目录下的index.php移到和public同级目录下,[或者直接在public同级目录下新建一个index.php] 第二步:那么这个新的index.php文件的内容如下: & ...

  8. Nginx安装负载均衡配置 fair check扩展

    前言 本文主要是针对Nginx安装.负载均衡配置,以及fair智能选举.check后端节点检查扩展功能如何扩展,进行讲解说明. fair模块: upstream-fair,“公平的”Nginx 负载均 ...

  9. dtcp格式定义

    common name type optional comment id string y Content id version string y DTCP version. "1.0&qu ...

  10. PHP学习 例外和错误处理

    <?phptry{ open_folder("C:\\book");}catch(Exception $ex) { echo 'Error Message:'.$ex-> ...