목차

숲속에서 새 구경하기

ps
링크acmicpc.net/…
출처BOJ
문제 번호25196
문제명숲속에서 새 구경하기
레벨골드 1
분류

애드혹

시간복잡도O(n^2)
인풋사이즈n<=2000
사용한 언어Python
제출기록32952KB / 3340ms
최고기록2748ms
해결날짜2022/11/12
출처

제1회 곰곰컵

풀이

코드

코드 1 - TLE

"""Solution code for "BOJ 25196. 숲속에서 새 구경하기".

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

import math
import dataclasses


@dataclasses.dataclass(order=True, slots=True)
class Interval:
    v: int = dataclasses.field(compare=False)
    s: int = dataclasses.field(compare=True)
    e: int = dataclasses.field(compare=False)


def main():
    a_interval = Interval(*[int(x) for x in input().split()])
    b_interval = Interval(*[int(x) for x in input().split()])
    c_interval = Interval(*[int(x) for x in input().split()])

    lcm = math.lcm(a_interval.v, b_interval.v, c_interval.v)
    intervals = [a_interval, b_interval, c_interval]
    first, second, third = sorted(intervals)
    while first.s <= lcm:
        if first.e < third.s:
            first.s += first.v
            first.e += first.v
        elif second.e < third.s:
            second.s += second.v
            second.e += second.v
        else:
            print(third.s)
            break
        first, second, third = sorted(intervals)
    else:
        print('-1')


if __name__ == '__main__':
    main()

코드 2 - AC

"""Solution code for "BOJ 25196. 숲속에서 새 구경하기".

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

import math


def main():
    A_v, A_s, A_e = [int(x) for x in input().split()]
    B_v, B_s, B_e = [int(x) for x in input().split()]
    C_v, C_s, C_e = [int(x) for x in input().split()]

    lcm = math.lcm(A_v, B_v, C_v)
    first, second, third = sorted(
        [[A_s, A_v, A_e - A_s], [B_s, B_v, B_e - B_s], [C_s, C_v, C_e - C_s]]
    )
    while first[0] <= lcm:
        if first[0] + first[2] < third[0]:
            first[0] += first[1]
            if first > third:
                first, second, third = second, third, first
            elif first > second:
                first, second = second, first
        elif second[0] + second[2] < third[0]:
            second[0] += second[1]
            if second > third:
                second, third = third, second
        else:
            print(third[0])
            break
    else:
        print('-1')


if __name__ == '__main__':
    main()