목차

Haybale Feast

ps
링크acmicpc.net/…
출처BOJ
문제 번호15459
문제명Haybale Feast
레벨골드 1
분류

투 포인터, 우선순위 큐, 이분탐색

시간복잡도O(nlogn)
인풋사이즈n<=100,000
사용한 언어Python
제출기록47084KB / 264ms
최고기록264ms
해결날짜2022/06/29

풀이

코드

코드 1 - 투포인터 + 단조증가 큐

"""Solution code for "BOJ 15459. Haybale Feast".

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

Tags: [Two pointer] [Monotone queue]
"""

import collections
import sys

INF = float('inf')


def main():
    N, M = [int(x) for x in sys.stdin.readline().split()]
    F_and_S = [[int(x) for x in sys.stdin.readline().split()] for _ in range(N)]

    left, right = iter(F_and_S), iter(F_and_S)
    tot_flavor = 0
    spiciness_deq = collections.deque()
    answer = INF
    while True:
        if tot_flavor < M:
            try:
                f, s = next(right)
            except StopIteration:
                break
            tot_flavor += f
            while spiciness_deq and spiciness_deq[-1] < s:
                spiciness_deq.pop()
            spiciness_deq.append(s)
        else:
            answer = min(answer, spiciness_deq[0])
            f, s = next(left)
            tot_flavor -= f
            if s == spiciness_deq[0]:
                spiciness_deq.popleft()

    print(answer)


if __name__ == '__main__':
    main()

코드 2 - 투포인터 + 우선순위큐

"""Solution code for "BOJ 15459. Haybale Feast".

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

Tags: [Two pointer] [Priority queue]
"""

import sys
from teflib import priorityqueue

INF = float('inf')


def main():
    N, M = [int(x) for x in sys.stdin.readline().split()]
    F_and_S = [[int(x) for x in sys.stdin.readline().split()] for _ in range(N)]

    spiciness_heap = priorityqueue.UpdatableHeap()
    left, right = iter(F_and_S), iter(F_and_S)
    tot_flavor = 0
    answer = INF
    while True:
        if tot_flavor < M:
            try:
                f, s = next(right)
            except StopIteration:
                break
            tot_flavor += f
            spiciness_heap.push(-s)
        else:
            answer = min(answer, -spiciness_heap.top())
            f, s = next(left)
            tot_flavor -= f
            spiciness_heap.delete(-s)

    print(answer)


if __name__ == '__main__':
    main()

코드 3 - Disjoint set

"""Solution code for "BOJ 15459. Haybale Feast".

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

Tags: [Disjoint set]
"""

import operator
import sys
from teflib import disjointset


def main():
    N, M = [int(x) for x in sys.stdin.readline().split()]
    F, S = zip(
        *([int(x) for x in sys.stdin.readline().split()] for _ in range(N)))

    dsu = disjointset.DisjointSet(N)
    flavor_by_set = list(F)
    for i, S_i in sorted(enumerate(S), key=operator.itemgetter(1)):
        flavor_cur = F[i]
        if i > 0 and S[i - 1] <= S_i:
            flavor_left = flavor_by_set[dsu.find(i - 1)]
            merged_set = dsu.union(i - 1, i)
            flavor_cur = flavor_by_set[merged_set] = flavor_cur + flavor_left
        if i + 1 < N and S[i + 1] <= S_i:
            flavor_right = flavor_by_set[dsu.find(i + 1)]
            merged_set = dsu.union(i + 1, i)
            flavor_cur = flavor_by_set[merged_set] = flavor_cur + flavor_right
        if flavor_cur >= M:
            print(S_i)
            break


if __name__ == '__main__':
    main()

코드 4 - 이분탐색

"""Solution code for "BOJ 15459. Haybale Feast".

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

Tags: [Binary search]
"""

import bisect
import sys

def main():

    def is_valid(s):
        f_sum = 0
        for f_i, s_i in F_and_S:
            if s_i > s:
                f_sum = 0
            else:
                f_sum += f_i
                if f_sum >= M:
                    return True
        return False

    N, M = [int(x) for x in sys.stdin.readline().split()]
    F_and_S = [[int(x) for x in sys.stdin.readline().split()] for _ in range(N)]

    s_sorted = sorted(set(s for f, s in F_and_S))
    min_s_ind = bisect.bisect_left(s_sorted, True, key=is_valid)
    print(s_sorted[min_s_ind])


if __name__ == '__main__':
    main()