Replace Sort
시간 제한3초메모리 제한1024 MB
B의 서로 다른 값을 각각 최대 한 번 사용해 A의 원소를 교체하여 A를 오름차순으로 정렬할 때, 필요한 최소 교체 횟수를 구한다.
문제
Consider an array and a set of integers such that all numbers in and are distinct. Your task is to turn into a sorted array. To do this you can take any number from and replace any element of with it. You can perform this operation any number of times, but each element of can be used at most once.
Determine the minimum number of operations needed to turn into a sorted array, or determine that it is impossible.
입력
The first line of input contains two integers and () --- the sizes of and respectively.
The second line contains integers .
The third line contains integers .
All the elements are distinct, positive and do not exceed .
출력
If it is impossible to turn into a sorted array, print . Otherwise, print the minimum number of operations needed.
힌트
In all three examples, the issue is that , so we have to change at least one of them.
In the first one, we can decrease by replacing it with , but it breaks the other side, so there is no solution.
In the second one, we also have , which we can use to fix the broken side. It is impossible to do with less than operations.
In the third example we can finally increase the last element, thus fixing in 1 operation.