RiseOJ는 solved.ac와 제휴 관계가 없습니다. 티어 아이콘 © solved.ac. solved.ac
포럼
COCI00350

Police

Unrated 레이팅 미적용
난이도
1s
시간 제한
64MB
메모리 제한
0
맞았습니다!!
0
제출 수
0.0%
정답률
레이팅

의견: 0

설명

\(7^{th}\) round, March \(7^{th}\) 2015
Librarian Jurica has \(N\) shelves in his library, and each shelf can contain \(M\) books. Jurica is a good
librarian so he decided to make an inventory in the library and, if it’s necessary, return the books that
aren’t in their place to their right place. He moves the books in the following way:
• moving the books one place to the left or to the right on a shelf if the place to the left or to the
right is available,
• taking a book from a shelf and placing it to an available place on that or any other shelf.
Careful Jurica can’t move books if he has a book in his hands. Additionally, he can’t take more than
one book at once.
Jurica has been having back pains ever since he had to move all the volumes of the printed edition
of Wikipedia from the first to the second floor so now he wants to put all the books in place with as
little lifting as possible because his back is hurting. What is the minimal number of lifting he needs?

제약

In test cases worth 50% of points in total, each book will be on the same shelf in the initial and final
state.

입력 형식

The first line of input contains the integers \(N\) and \(M\) (1 ⩽\(N\) ⩽1 000, 1 ⩽\(M\) ⩽1 000).
Each of the following \(N\) lines contains \(M\) integers, the \(i\)^{th} line describing the current state of the \(i\)^{th}
shelf.
Number 0 denotes an empty place on the shelf, and a number different than 0 denotes that there is a
book in that place denoted with that number. All books are denoted with different numbers from 1
to \(K\), where \(K\) is the total number of books on the shelves. After that, an additional \(N\) lines follow,
each containing \(M\) integers, the \(i\)^{th} line describing the wanted state of the \(i\)^{th} shelf.
In the initial and final state of the shelves, the same books will appear.

출력 형식

The first and only line of output must contain the required minimal number of lifting \(or -1\) if it is
impossible to arrange the books in the aforementioned way.

예제 1
입력
2 4
1 0 2 0
3 5 4 0
2 1 0 0
3 0 4 5
3 3
1 2 3
4 5 6
7 8 0
4 2 3
6 5 1
0 7 8
2 2
1 2
3 4
2 3
4 1
출력
output
output
2
4
-1
설명

Clarification of the first example: Jurica will move book 1 one place to the right, then lift book 2 and move
it over to the first place of the first shelf. He lifts book 5 and places it on the fourth place on the second shelf.

문제 정보

생성자가 기록되지 않았습니다.

출처 COCI 2014/2015 Contest 7

평가 및 의견

Police

개요
출제자 난이도 Unrated 레이팅 미적용 의견 0 / 50 공개 집계 (커뮤니티 난이도, 주요 주제, 품질)는 의견이 충분히 모이면 공개됩니다.

Log in to rate problems.

개별 의견

아직 의견이 없습니다. 자격이 된다면 위 양식에서 가장 먼저 평가해 보세요.

풀이 제출

Police

게스트로 둘러보고 있습니다. 로그인하면 풀이를 제출하고 진행 상황을 확인할 수 있습니다. 로그인하고 제출하기
공개
C++20 Tab 들여쓰기 · Ctrl+/ 주석 토글 · Enter 자동 들여쓰기
1 1 1 0 공백: 4 · UTF-8