これがあなたが従うことができるアプローチです。あなたが与えられたとしましょう
[1 から N] の範囲。ここで、N は可変 (ユーザーからの入力)/定数 (固定済み) にすることができます。
サブセットは Ai[ai, bi] で、1<=i<=k です。つまり、ai が下限で bi が上限である k 個のサブセットがあります。
ここで、最初のノードを [1,N] としてツリーの作成を開始します。アルゴリズムの実行中の特定の時点で、ツリー内の各リーフ ノードは、特定のサブセットのいずれにも含まれない数値の範囲を表します。つまり、すべてのリーフ ノードの数値の範囲を出力する必要があります。
初期条件
まず、ツリーには葉ノード [1,N] が 1 つしかありません。つまり、サブセットをまだ処理していないため、1 から N までのすべての数値を出力する必要があります。
終了条件
アルゴリズム ツリーの最後には多くのリーフが含まれます。各リーフは、サブセットによってカバーされていない数値の範囲を表します。したがって、これらの数値を出力として出力する必要があります。
アルゴリズム:
STEP 1: Creating the Tree
For i = 1 to k //process for all given subsets
{
For every leaf node in current tree
{
//Let [x,y] is the current leaf node being processed
1. if(ai>=x && bi<=y) //subset being processed lie inside the leaf being
processed.
create nodes [x,ai-x] and [bi+1,y] and attach as child of the leaf node
being processed.
2. if((x<=ai<y) && (bi>y)) //subset overflows towards right
create a node [x, ai-1] and attach as child to the current leaf node being
processed.
3. if((ai<x) && (x<bi<=y)) //subset overflows towards left
create a node [bi+1, y] and attach as child to the current leaf node being
processed.
}
}
STEP 2: Printing the output
//Now the leaf nodes indicate the numbers to be printed.
For each leaf node [x,y] of the resulting tree
{
//you will get some leaf node with x>y
if(x<=y)
print the numbers in range [x,y].
}