luogu#P7796. [COCI 2014/2015 #7] POLICE
[COCI 2014/2015 #7] POLICE
Problem Description
In librarian Jurica's library, there are bookshelves, and each bookshelf has positions, numbered from to from left to right. Each position can hold one book. Jurica is a good librarian, so he decides to take inventory of the library and, if necessary, return books that are not in their correct positions to where they originally belonged. Specifically, the book currently placed at position on bookshelf is numbered (if , it means the position is currently empty), while the book that originally belonged to this position is numbered (if , it means the position was originally empty). He moves books using the following operations:
- Operation : If there is an empty position to the left or right, a book can be moved left or right along the same bookshelf.
- Operation : Take a book off a bookshelf and place it into an empty position on the same bookshelf or on another bookshelf.
Careful Jurica cannot move books while holding a book in his hands. Also, he cannot hold more than one book at the same time.
Ever since he had to move all volumes of the printed edition of Wikipedia from the first floor to the second floor, Jurica has had back pain. Because his back hurts, he now wants to put all books into their correct places with as little carrying as possible. Please tell him the minimum number of times he needs to perform operation , or tell him that it is impossible to restore all books to their original positions.
Input Format
The input consists of lines.
The first line contains two integers , representing the number of bookshelves and the number of positions on each bookshelf.
Lines to each contain integers , describing the current arrangement of books on the shelves.
Lines to each contain integers , describing the original arrangement of books on the shelves.
The input guarantees that the sets of books appearing in the original and current states are exactly the same.
Output Format
If Jurica cannot restore all books to their original positions, output a single integer .
Otherwise, output a single integer on one line, representing the minimum number of times operation needs to be performed.
2 4
1 0 2 0
3 5 4 0
2 1 0 0
3 0 4 5
2
3 3
1 2 3
4 5 6
7 8 0
4 2 3
6 5 1
0 7 8
4
2 2
1 2
3 4
2 3
4 1
-1
Hint
[Sample 1 Explanation]
Below is Jurica's sequence of operations:
- Operation : Move book to the position on its right, i.e. the second position on its bookshelf.
- Operation : Carry book to the first position on its bookshelf.
- Operation : Carry book to the second position on its bookshelf.
It can be proven that there is no solution with fewer executions of operation .
[Constraints]
For of the testdata, each book is guaranteed to stay on the same bookshelf in both the initial and final states.
For all testdata, .
[Source]
This problem is from COCI 2014-2015 CONTEST 7 T6 POLICE, using the original testdata settings, with a full score of points.
Translated and整理 (pinyin: zhengli) by Eason_AC.
Translated by ChatGPT 5