CF1700F.Puzzle
省选/NOI-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 (x1,y1) and (x2,y2) neighboring if ∣x1−x2∣+∣y1−y2∣=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.
学生爱丽丝和易卜拉欣是最好的朋友。易卜拉欣的生日快到了,因此爱丽丝决定送他一个新谜题作为礼物。该谜题可表示为一个 2 行 n 列的矩阵,其中每个元素均为 0 或 1。每次操作允许你交换两个相邻单元格中的值。
更准确地说,我们从上到下将行编号为 1 至 2,从左到右将列编号为 1 至 n。同时,记第 x 行、第 y 列的单元格为 (x,y)。若 ∣x1−x2∣+∣y1−y2∣=1,则称单元格 (x1,y1) 与 (x2,y2) 相邻。
爱丽丝不喜欢当前单元格的排列方式,因此她设计了一种自己期望的排列,并希望以此作为礼物送给易卜拉欣。由于你是最聪明的朋友,她请你帮忙找出实现目标排列所需的最少操作次数;若无法达成目标排列,则需判定其不可行。
输入格式
The first line contains an integer n (1≤n≤200000) — 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.
第一行包含一个整数 n(1≤n≤200000)—— 表示谜题中列的数量。
接下来的两行描述谜题当前的布局。每行包含 n 个整数,每个整数为 0 或 1。
最后两行以相同格式描述 Alice 所期望的布局。
输出格式
If it is possible to get the desired arrangement, print the minimal possible number of steps, otherwise print −1.
如果可以得到目标排列,则输出最少的操作步数;否则输出 −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)。
可以证明,此时最小可能的答案为 5。
在第二个例子中,无论进行何种交换,都无法得到目标排列,因此答案为 −1。
输入解题思路,AI测评打分。不知道怎么写?