Fişierul intrare/ieşire:minperm.in, minperm.outSursăIIOT 2021-22 Runda 4
AutorGeorge RapeanuAdăugată destefdascalescuStefan Dascalescu stefdascalescu
Timp execuţie pe test0.5 secLimită de memorie131072 kbytes
Scorul tăuN/ADificultateN/A

Vezi solutiile trimise | Statistici

Minperm

The beautiful city of Julcvari decided to host a city tour(online, of course). They have n different attractions, the i`th one having a beauty equal to i. An initial order for the presentation of the attractions has been selected, but the people of Julcvari started worrying that it might not be the best one.

They believe that the best order for visiting the attractions is the one for which the number of pairs of attractions (i, j), where i was visited before j but i is more beautiful than j is minimised. But organizing events is not so easy, a lot of paperwork needs to be done in order to change something like this.

So, the host managed to get k swap plans: let l_1, l_2, ..., l_k be an array of integers representing the swap plans. The host can perform any number of swaps on the initial order, such that the swapped positions are at a distance equal to one of the elements from array l. There is not much time left, so the people of Julcvari ask you for help in finding the best order in which the attractions are presented.

In other words, a permutation of size n and an array of size k with distinct elements are given. You can only perform swaps between positions i and j such that |j - i| is equal to an element of l. What is the permutation with the minimum number of inversions that can be obtained?

Date de intrare

The first line of the input file minperm.in contains n and k.

The second line of the input contains n integers, representing the initial order of the attractions.

The third line of the input contains k integers, representing the k available swap plans.

Date de ieşire

The only line of the output file minperm.out should contain n elements, representing the best order for showing the attractions.

Restricţii

  • 1 ≤ k ≤ n ≤ 5000
  • For tests worth 10 points, 1 ≤ k ≤ n ≤ 8
  • For tests worth 30 more points, 1 ≤ k ≤ n ≤ 100

Exemplu

minperm.inminperm.out
8 1
1 4 3 5 7 2 8 6
8
1 4 3 5 7 2 8 6
minperm.inminperm.out
8 7
2 4 1 5 3 6 7 8
7 6 3 8 2 5 1
1 2 3 4 5 6 7 8
Trebuie sa te autentifici pentru a trimite solutii. Click aici

Cum se trimit solutii?