AlgoLensArena
ContestlərProblemlərOtaqlar
Vizualizator
← Məsələlər

BAPC20-056.\problemyamlname

Medium · 1 s · 256 MB

  • icpc
  • bapc
  • 2020

You are an employee at the Benelux Aquarium for Piranhas and Catfish. The aquarium 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,n1,…,n from left to right. The exhibit contains kkk 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 222, 777 and 999. If you stick your finger into the water at position 444, the piranhas will be at positions 333, 666 and 999 after one second. You now have to pull your finger away to prevent the piranha at position 333 from biting your finger one second later. If you now stick your finger into the water at position 111, only the piranha at position 333 will move and will end up at position 2 after one second.

Giriş formatı

The input consists of:

• One line containing two integers nnn (1≤n≤10001\leq n\leq10001≤n≤1000), the number of positions, and kkk (1≤k≤n1\leq k\leq n1≤k≤n), the number of piranhas.

• One line containing kkk integers 1≤p1<…<pk≤n1\leq p_1<\ldots<p_k\leq n1≤p1​<…<pk​≤n, the current positions of the piranhas.

• One line containing kkk integers 1≤d1<…<dk≤n1\leq d_1<\ldots<d_k\leq n1≤d1​<…<dk​≤n, the desired positions of the piranhas.

Çıxış formatı

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”.

Qeyd

Source: Benelux Algorithm Programming Contest 2020 · Author: Freek Henstra · License: CC BY-SA 4.0 · Original: https://bapc.eu · Statement adapted for AlgoLens and shared under the same license.

Nümunələr

Giriş #1

9 3
3 7 9
3 5 9

Çıxış #1

4

Giriş #2

8 3
1 5 8
2 4 7

Çıxış #2

impossible

Giriş #3

20 6
1 4 7 10 13 20
2 5 8 11 14 17

Çıxış #3

17