BOJ - 2170 - 선 긋기

Updated:

import sys

def solution():
    N = int(sys.stdin.readline())

    lines = []
    for _ in range(N):
        start, end = map(int, sys.stdin.readline().split())
        lines.append((start, end))

    lines.sort(key=lambda x:(x[0], -x[1]))
    ret = lines[0][1] - lines[0][0]
    start, end = lines[0][0], lines[0][1]
    del lines[0]
    for s, e in lines:
        if s >= end:
            ret += (e - s)
            start = s
            end = e
        elif start <= e <= end:
            continue
        elif s <= end and e >= end:
            ret += (e - end)
            end = e

    print(ret)

solution()

https://www.acmicpc.net/problem/2170

Categories:

Updated:

Leave a comment