【AGC006E】 Rotate 3x3
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的更多相关文章
- 【leetcode】Rotate Image
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- 【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 ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【LeetCode】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
- 【题解】【矩阵】【回溯】【Leetcode】Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【链表】Rotate List(三个指针)
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...
- 【数组】Rotate Image
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- 【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 ...
- 【Leetcode】【Medium】Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
随机推荐
- SE93 创建参数事务
(1)SE93 输入新的事务码名称,点击创建按钮 (2)输入事务码描述,选择第五项: Transaction with parameters(parameter transaction) (3)在事务 ...
- 20155209 Exp5 MSF基础应用
Exp5 MSF基础应用 实验准备 在实验之前,上网搜集了很多有关Metasploit渗透测试的资料.对这次实验影响最大的是一篇最受欢迎的10个Metasploit模块和插件.排名第一位的是MSB-M ...
- 20155217《网络对抗》Exp05 MSF基础应用
20155217<网络对抗>Exp05 MSF基础应用 实践内容 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 一个主动攻击实践,如ms ...
- 5.Xilinx RapidIO核例子工程源码分析
https://www.cnblogs.com/liujinggang/p/10091216.html 一.软件平台与硬件平台 软件平台: 操作系统:Windows 8.1 64-bit 开发套件:V ...
- mybatis 初步使用(IDEA的Maven项目, 超详细)
目录 创建 Maven 项目 Maven配置 pom.xml 创建数据库 配置Mybatis 配置mybatis的XML文件 创建实体类和对应的Mapper.xml 测试 源码 @ 创建 Maven ...
- Java设计模式-建造者(Builder)模式
目录 由来 使用 1. 定义抽象 Builder 2. 定义具体 Builder类 3. 定义具体 Director类 4. 测试 定义 文字定义 结构图 优点 举例 @ 最近在看Mybatis的源码 ...
- 移动端H5页面上传图片或多张图片
传统PC网页上传文件,大家都已经熟悉,这里不做介绍. 本文简单介绍移动端常用上传图片功能.灵活使用轮询或长连接可实现PC与移动端数据同步,即PC端需要上传的图片是移动拍照下来或移动端硬盘储存的,不需要 ...
- 设计模式 笔记 备忘录模式 Memento
//---------------------------15/04/27---------------------------- //Memento 备忘录模式----对象行为型模式 /* 1:意图 ...
- CM005-逆向分析过程(上篇)
前言 005,都说比较变态,很多人给放过去了,但是我还是决定上了它,既然变态就分两篇,上篇先实际说流程,到底应该怎么上它,下篇会告诉逆向分析的过程和方法 准备 [环境和工具] win7/xp虚拟机环境 ...
- PAT甲题题解-1069. The Black Hole of Numbers (20)-模拟
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789244.html特别不喜欢那些随便转载别人的原创文章又不给 ...