到着と出発は「イベント」であり、配列にはそれらのイベントの時刻が含まれます。基本的なロジックは、次のイベントの時刻を見つけて、そのイベントに関連付けられた更新を実行することです。着信側では、キューの長さが増えます。離脱の場合、キューの長さは減分されます。あなたが整数倍でのみ印刷する場合
a = [1.1, 2.9, 5.1, 6.5]
d = [3.5, 5.2, 7.2, 8.0]
queue = 0
a_index = 0
d_index = 0
print(0, ':', queue)
while a_index < len(a) and d_index < len(d):
if a[a_index] < d[d_index]: # did an arrival come first?
queue += 1
print(a[a_index], ':', queue)
a_index += 1
else:
queue -= 1
print(d[d_index], ':', queue)
d_index += 1
# ran out of elements in one of the arrays,
# iterate through remainder of the other
while a_index < len(a):
queue += 1
print(a[a_index], ':', queue)
a_index += 1
while d_index < len(d):
queue -= 1
print(d[d_index], ':', queue)
d_index += 1
:以下は、それが変更されるたびに、時間とキューの長さを出力し、そのアイデアのかなり強引な実装(あなたが指定されていませんでしたので、のpython3)でありますこれらをイベントとしても設定してください:
a = [1.1, 2.9, 5.1, 6.5]
d = [3.5, 5.2, 7.2, 8.0]
queue = 0
a_index = 0
d_index = 0
print_time = 1
max_time = 10
print(0, ':', queue)
while a_index < len(a) and d_index < len(d):
if a[a_index] < d[d_index] and a[a_index] <= print_time:
queue += 1
a_index += 1
elif d[d_index] <= print_time:
queue -= 1
d_index += 1
else:
print(print_time, ':', queue)
print_time += 1
while a_index < len(a):
if a[a_index] <= print_time:
queue += 1
a_index += 1
else:
print(print_time, ':', queue)
print_time += 1
while d_index < len(d):
if d[d_index] <= print_time:
queue -= 1
d_index += 1
else:
print(print_time, ':', queue)
print_time += 1
while print_time <= max_time:
print(print_time, ':', queue)
print_time += 1
これは間違いなく緊張しているかもしれませんが、アプローチを伝えるものです。
この数よりも多くのイベントがあった場合は、優先順位の高いキューにイベントを配置し、発生時間順にイベントを配置し、イベントを1つずつ取り出し、適切なイベントにディスパッチしますどのイベントタイプが発生したかに基づいて、状態遷移ロジックを生成する。このアプローチのロジックはthis paperに記載されています。このペーパーはJavaでアイデアを実装していますが、Python3の実装とデモキューイングモデルはhereです。
https://stackoverflow.com/questions/45972684/get-a-list-of-overlapping-time-ranges-from-a-set-of-appointments/45973161#45973161 – MBo
返信いただきありがとうございます。私はまだ到着/出発時間の行列を反復することについて混乱しています - 私は重なりを探すのではなく、いつでも店舗内の顧客の数を把握する方法です。 –
キューの長さの値がすべて発生したときにプロットするのではなく、単位時間のティックだけをプロットする必要がありますか?後者は実際にはもっと面白いです。 – pjs