実行時間制限: 3 sec / メモリ制限: 1024 MB
配点 : 点
問題文
黒板に 以上 以下の整数からなる集合 個 が書かれています。ここで、 です。
あなたは、以下の操作を好きな回数( 回でもよい)行うことが出来ます。
- 個以上の共通した要素を持つ 個の集合 を選ぶ。 の 個を黒板から消し、新たに を黒板に書く。
ここで、 とは か の少なくともどちらかに含まれている要素のみからなる集合を意味します。
と が両方含まれる集合を作ることが出来るか判定してください。出来るならば、必要な操作回数の最小値を求めてください。
制約
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
出力
と が両方含まれる集合を作ることが出来るならば必要な操作回数の最小値を、出来ないならば -1
を出力せよ。
入力例 1 Copy
3 5 2 1 2 2 2 3 3 3 4 5
出力例 1 Copy
2
まず、 と を選んで消し、 を追加します。
そして、 と を選んで消し、 を追加します。
すると 回の操作で と を両方含む集合を作ることが出来ます。 回の操作では目標を達成できないため、答えは です。
入力例 2 Copy
1 2 2 1 2
出力例 2 Copy
0
始めから が を共に含むため、必要な操作回数の最小値は 回です。
入力例 3 Copy
3 5 2 1 3 2 2 4 3 2 4 5
出力例 3 Copy
-1
入力例 4 Copy
4 8 3 1 3 5 2 1 2 3 2 4 7 4 4 6 7 8
出力例 4 Copy
2
Score : points
Problem Statement
On a blackboard, there are sets consisting of integers between and . Here, .
You may perform the following operation any number of times (possibly zero):
- choose two sets and with at least one common element. Erase them from the blackboard, and write on the blackboard instead.
Here, denotes the set consisting of the elements contained in at least one of and .
Determine if one can obtain a set containing both and . If it is possible, find the minimum number of operations required to obtain it.
Constraints
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
If one can obtain a set containing both and , print the minimum number of operations required to obtain it; if it is impossible, print -1
instead.
Sample Input 1 Copy
3 5 2 1 2 2 2 3 3 3 4 5
Sample Output 1 Copy
2
First, choose and remove and to obtain .
Then, choose and remove and to obtain .
Thus, one can obtain a set containing both and with two operations. Since one cannot achieve the objective by performing the operation only once, the answer is .
Sample Input 2 Copy
1 2 2 1 2
Sample Output 2 Copy
0
already contains both and , so the minimum number of operations required is .
Sample Input 3 Copy
3 5 2 1 3 2 2 4 3 2 4 5
Sample Output 3 Copy
-1
Sample Input 4 Copy
4 8 3 1 3 5 2 1 2 3 2 4 7 4 4 6 7 8
Sample Output 4 Copy
2