Logo
(追記) (追記ここまで)

26462번 - 1-Player Concentration 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 1024 MB35312586.207%

문제

You got $HW$ cards, where $HW$ is even. Each card has a number 1ドル$ through $HW/2$ on the face, and exactly two cards have the same number. You considered what types of card games you could play with the cards, and decided to play 1-player concentration.

In the 1-player concentration, you first align $HW$ cards face down on the $H \times W$ rectangle field. Your goal is to remove all the cards from the field by repeating turns. For each turn, you must flip exactly two cards. If the two flipped cards have the same number on the face, you remove the two cards from the field. If not, you flip the two cards again to make them face down. But because you have perfect memory, you can remember which card has which number and where it's placed if you flipped the card once. So for each turn, you will act as follows.

  1. If it's the second or later turn and you already know where two cards having the same number are placed according to previous turns, flip the two cards and remove them from the field.
  2. If not, flip a card you haven't flipped yet and with the highest precedence (We define the precedence of cards later). If you have seen the number on the card on another already flipped card, flip the other card and remove the two cards from the field.
  3. If not, flip another card you haven't flipped yet and with the highest precedence. If the first and the second cards you flipped in this turn (luckily) have the same number, remove the two cards from the field.
  4. If not, make the two flipped cards face down to prepare for the next turn.

Let's number the rows 1ドル$ through $H$ from the top. The card at the topmost row among the remaining cards has the highest precedence. If there are multiple cards at the topmost row, if the row is initially an odd-numbered row, the leftmost card has the highest precedence. If the row is initially an even-numbered row, the rightmost card has the highest precedence.

After you played the game, you noticed you forgot to count how many turns you took to remove all the cards from the field. Fortunately, you remember the initial placement of the cards. So you decided to write a program to compute the turns you take to remove all the cards for a given initial placement.

입력

The first line contains two integers $H$ (1ドル ≤ H ≤ 100$) and $W$ (1ドル ≤ W ≤ 100$). You can assume $H \times W$ is even. Each of the following $H$ lines has exactly $W$ integers. The $j$-th integer of the $i$-th row represents the number on the face of a card at the $i$-th from the top and the $j$-th from the left. You can assume all the integers in the $H$ lines are no less than 1ドル,ドル no more than $HW/2,ドル and the same integer appears exactly twice.

출력

Output in a line a single integer, which is the number of turns you take to remove all the cards.

제한

예제 입력 1

2 6
5 5 1 4 4 1
6 2 3 2 3 6

예제 출력 1

9

예제 입력 2

4 3
1 1 2
3 3 2
4 4 5
6 6 5

예제 출력 2

6

예제 입력 3

1 10
1 2 3 4 5 5 4 3 2 1

예제 출력 3

7

힌트

출처

Contest > ICPC Japanese Alumni Group > JAG Practice Contest for ICPC Asia Regional > JAG Practice Contest for ICPC 2022 Asia Yokohama Regional B번

Camp > Petrozavodsk Programming Camp > Winter 2024 > Day 1: Welcome Contest B번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /