목차

Starting a Scenic Railroad Service

ps
링크acmicpc.net/…
출처BOJ
문제 번호15337
문제명Starting a Scenic Railroad Service
레벨플래티넘 3
분류

스위핑

시간복잡도O(n)
인풋사이즈n<=200,000
사용한 언어Python 3.11
제출기록82368KB / 704ms
최고기록704ms
해결날짜2023/01/10

풀이

코드

"""Solution code for "BOJ 15337. Starting a Scenic Railroad Service".

- Problem link: https://www.acmicpc.net/problem/15337
- Solution link: http://www.teferi.net/ps/problems/boj/15337

Tags: [Sweeping]
"""

import sys

ALIGHTING, BOARDING = 0, 1


def main():
    n = int(sys.stdin.readline())
    events = []
    for i in range(n):
        a, b = [int(x) for x in sys.stdin.readline().split()]
        events.append((a, BOARDING, i))
        events.append((b, ALIGHTING, i))

    events.sort()
    s2 = 0
    overlapping_section_count = [0] * n
    boarding_count = alighting_count = 0
    for _, type_, i in events:
        if type_ == BOARDING:
            boarding_count += 1
            overlapping_section_count[i] = -alighting_count
            s2 = max(s2, boarding_count - alighting_count)
        else:
            alighting_count += 1
            overlapping_section_count[i] += boarding_count
    s1 = max(overlapping_section_count)

    print(s1, s2)


if __name__ == '__main__':
    main()