ABC256 D - Union of Interval
https://atcoder.jp/contests/abc256/tasks/abc256_d
提出
WA
code: python
n = int(input())
lr = list(map(int, input().split())) for _ in range(n)
# minとmaxの保持
lr.sort()
tmpl = lr00
tmpr = lr01
for i in range(1, n):
if (lri0 <= tmpr):
continue
else:
print(tmpl, lri1)
tmpl = lri0
tmpr = lri1
解答
code: python
n = int(input())
lr = list(map(int, input().split())) for _ in range(n)
lr.sort()
# 今の区間左側
nowL = lr00
# 今の区間右側
nowR = lr01
for i in range(1,n):
# 次の(i番目の)区間左側
nextL = lri0
# 次の(i番目の)区間右側
nextR = lri1
# 「今の右側」<「次の左側」
if nowR < nextL:
# [nowL,nowR)は絶対に必要な区間
print(nowL, nowR)
# 今の区間を更新
nowL = nextL
nowR = nextR
# それ以外(「次の左側」≤「今の右側」)
else:
# 「今の右側」<「次の右側」
if nowR < nextR:
# 区間を更新(右側を広げる)
nowR = nextR
# 最後に区間を出力
print(nowL, nowR)
メモ
東京海上日動プログラミングコンテスト2022(ABC256) A~D問題 ものすごく丁寧でわかりやすい解説 python 灰色~茶色コーダー向け