목차

Blindfold Nim

ps
링크acmicpc.net/…
출처BOJ
문제 번호8318
문제명Blindfold Nim
레벨플래티넘 3
분류

그리디

시간복잡도O(nlogm)
인풋사이즈n<=1,000,000, m<=1,000,000
사용한 언어Python 3.11
제출기록55292KB / 884ms
최고기록884ms
해결날짜2023/12/13

풀이

코드

"""Solution code for "BOJ 8318. Blindfold Nim".

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

Tags: [game theory]
"""

import heapq


def main():
    n = int(input())
    a = [int(x) for x in input().split()]

    answer = 0
    p = 1
    heap = [-a_i for a_i in a]
    heapq.heapify(heap)
    while heap:
        player_stack = -heapq.heappop(heap)
        p *= player_stack / (player_stack + 1)
        if player_stack > 1:
            heapq.heappush(heap, -(player_stack - 1))

        if not heap:
            answer += p
            break

        opponent_stack = -heapq.heappop(heap)
        answer += p / (opponent_stack + 1)
        p *= opponent_stack / (opponent_stack + 1)
        if opponent_stack > 1:
            heapq.heappush(heap, -(opponent_stack - 1))

    print(answer)


if __name__ == '__main__':
    main()