Pizza Party

시간 제한4초메모리 제한1024 MB

요약
피자 배열과 각 사람이 원하는 맛이 주어질 때, 모든 사람이 원하는 맛을 받도록 피자를 스택에 배치하고 최소 개수의 스택을 구한다.
난이도

어려움10점 중 8점

유형
그리디, 스택, 구현, 배열
정답자
아직 제출이 없습니다

문제

Troy is fleshing out the details of his latest initiative, HackCCO! Everyone knows that the biggest appeal of any hackathon is the free food. As such, to ensure the unparalleled success of HackCCO, Troy ordered a comically large cart stacked with NN pizzas where the ii-th pizza from the top of the cart has flavour a_ia\_i.

After the pizza cart arrives, Troy needs to arrange them into some number of stacks on a long table. To do this, he takes the pizzas one-by-one from the top of the cart and moves them onto the table, each time either placing the pizza on top of another stack of pizzas or forming a new stack on the table.

The NN hungry HackCCO participants are lined up to get pizza from the table, one-by-one. Troy knows that the ii-th person in line has a favourite pizza flavour of b_ib\_i. When the ii-th person walks up to the table, if they see any pizzas of their favourite flavour at the top of any stack they will take any one of them at random. Otherwise, they won’t take anything and will leave the table hungry.

Of course, hungry coders are not happy coders, so Troy doesn’t want anyone to leave the table hungry. Thus, he asks you to help him find an arrangement of pizzas on the table such that it is possible for nobody to leave hungry. Furthermore, out of all such arrangements, Troy wants you to find one that creates the smallest number of stacks on the table (after all, tables can only get so long). Help him find such an arrangement or determine that it’s impossible!

입력

The first line of input contains a single integer NN.

The second line of input contains NN space-separated integers a_1,…,a_Na\_1, \dots , a\_N (1≤a_i≤N1 ≤ a\_i ≤ N).

The third line of input contains NN space-separated integers b_1,…,b_Nb\_1, \dots , b\_N (1≤b_i≤N1 ≤ b\_i ≤ N).

출력

If it is impossible to arrange the pizzas as desired, output -1.

Otherwise, your output should consist of three lines. On the first line output KK, the minimum number of stacks required. On the second line output NN space-separated integers c_1,…,c_Nc\_1, \dots , c\_N (1≤c_i≤K1 ≤ c\_i ≤ K), indicating that the ii-th pizza should be placed on stack c_ic\_i. On the third line output NN space-separated integers d_1,…,d_Nd\_1, \dots , d\_N (1≤d_i≤K1 ≤ d\_i ≤ K), indicating that the ii-th person in line takes their pizza from the d_id\_i-th stack. This stack must have a pizza of flavour b_ib\_i at the top when the ii-th participant walks up to get their pizza.

예제2

  1. 예제 1

    입력
    7
    1 2 3 2 2 1 3
    2 3 1 2 3 2 1
    
    예상 출력
    2
    1 2 1 2 1 2 2
    1 2 2 2 1 2 1
    
  2. 예제 2

    입력
    2
    1 2
    1 1
    
    예상 출력
    -1