A15234 | Puzzle
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Pupils Alice and Ibragim are best friends. It's Ibragim's birthday soon, so Alice decided to gift him a new puzzle. The puzzle can be represented as a matrix with $2$ rows and $n$ columns, every element of which is either $0$ or $1$ . In one move you can swap two values in neighboring cells.
More formally, let's number rows $1$ to $2$ from top to bottom, and columns $1$ to $n$ from left to right. Also, let's denote a cell in row $x$ and column $y$ as $(x, y)$ . We consider cells $(x_1, y_1)$ and $(x_2, y_2)$ neighboring if $|x_1 - x_2| + |y_1 - y_2| = 1$ .
Alice doesn't like the way in which the cells are currently arranged, so she came up with her own arrangement, with which she wants to gift the puzzle to Ibragim. Since you are her smartest friend, she asked you to help her find the minimal possible number of operations in which she can get the desired arrangement. Find this number, or determine that it's not possible to get the new arrangement.
More formally, let's number rows $1$ to $2$ from top to bottom, and columns $1$ to $n$ from left to right. Also, let's denote a cell in row $x$ and column $y$ as $(x, y)$ . We consider cells $(x_1, y_1)$ and $(x_2, y_2)$ neighboring if $|x_1 - x_2| + |y_1 - y_2| = 1$ .
Alice doesn't like the way in which the cells are currently arranged, so she came up with her own arrangement, with which she wants to gift the puzzle to Ibragim. Since you are her smartest friend, she asked you to help her find the minimal possible number of operations in which she can get the desired arrangement. Find this number, or determine that it's not possible to get the new arrangement.
输入格式
The first line contains an integer $n$ ( $1 \leq n \leq 200\,000$ ) — the number of columns in the puzzle.
Following two lines describe the current arrangement on the puzzle. Each line contains $n$ integers, every one of which is either $0$ or $1$ .
The last two lines describe Alice's desired arrangement in the same format.
Following two lines describe the current arrangement on the puzzle. Each line contains $n$ integers, every one of which is either $0$ or $1$ .
The last two lines describe Alice's desired arrangement in the same format.
输出格式
If it is possible to get the desired arrangement, print the minimal possible number of steps, otherwise print $-1$ .
输入输出样例
输入 #1
5 0 1 0 1 0 1 1 0 0 1 1 0 1 0 1 0 0 1 1 0
输出 #1
5
输入 #2
3 1 0 0 0 0 0 0 0 0 0 0 0
输出 #2
-1
In the first example the following sequence of swaps will suffice:
- $(2, 1), (1, 1)$ ,
- $(1, 2), (1, 3)$ ,
- $(2, 2), (2, 3)$ ,
- $(1, 4), (1, 5)$ ,
- $(2, 5), (2, 4)$ .
It can be shown that $5$ is the minimal possible answer in this case.
In the second example no matter what swaps you do, you won't get the desired arrangement, so the answer is $-1$ .
- $(2, 1), (1, 1)$ ,
- $(1, 2), (1, 3)$ ,
- $(2, 2), (2, 3)$ ,
- $(1, 4), (1, 5)$ ,
- $(2, 5), (2, 4)$ .
It can be shown that $5$ is the minimal possible answer in this case.
In the second example no matter what swaps you do, you won't get the desired arrangement, so the answer is $-1$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted