아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Aquarium

면접 대비

시간 제한2초메모리 제한512 MB

요약
빈 위치에 손가락을 넣어 좌우에서 가장 가까운 피라냐를 유인하는 조작을 반복해, 피라냐를 원하는 위치로 옮기는 최소 시간을 구한다. 불가능하면 impossible을 출력한다.
난이도

보통10점 중 6점

유형
BFS, 구현, 시뮬레이션
정답자
아직 제출이 없습니다

문제

The aquarium at which you work is hoping to expand its meagre selection of aquatic life, but lacks the funds to do so. You have been tasked to help promote the aquarium by taking photos of the two exhibits. Taking the first photo went swimmingly, because the catfish were very cooperative. For the piranhas, you have an arrangement of piranhas in mind that will look great on the photo. However, the only way to get the piranhas to move is by recklessly sticking your finger into the water to lure the piranhas. Your goal is to move the piranhas to the desired positions as quickly as possible without losing your finger in the process.

The piranha exhibit can be divided into positions 1,…,n1,\ldots,n from left to right. The exhibit contains kk piranhas and every position is occupied by at most one piranha. You can stick your finger into any unoccupied position. This will lure the nearest piranha to the left of your finger and the nearest piranha to the right of your finger. These piranhas will swim towards your finger, moving forward one position per second. All other piranhas simply stay in place. A piranha will bite your finger if it reaches the same position, so you must pull your finger away before this happens. Pulling your finger away and sticking it into a different position does not take any time.

For example, suppose there are piranhas at positions 22, 77 and 99. If you stick your finger into the water at position 44, the piranhas will be at positions 33, 66 and 99 after one second. You now have to pull your finger away to prevent the piranha at position 33 from biting your finger one second later. If you now stick your finger into the water at position 11, only the piranha at position 33 will move and will end up at position 2 after one second.

입력

  • One line containing two integers nn (1≤n≤10001\leq n\leq1000), the number of positions, and kk (1≤k≤n1\leq k\leq n), the number of piranhas.
  • One line containing kk integers 1≤p_1<…\<p_k≤n1\leq p\_1<\ldots\<p\_k\leq n, the current positions of the piranhas.
  • One line containing kk integers 1≤d_1<…\<d_k≤n1\leq d\_1<\ldots\<d\_k\leq n, the desired positions of the piranhas.

출력

Output the minimum number of seconds needed to get all of the piranhas at the desired positions. If it is impossible to do so, output "impossible".

예제3

  1. 예제 1

    입력
    9 3
    3 7 9
    3 5 9
    
    예상 출력
    4
    
  2. 예제 2

    입력
    8 3
    1 5 8
    2 4 7
    
    예상 출력
    impossible
    
  3. 예제 3

    입력
    20 6
    1 4 7 10 13 20
    2 5 8 11 14 17
    
    예상 출력
    17