목차

Stones

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

게임 이론

시간복잡도O(n)
인풋사이즈n<=500
사용한 언어Python 3.11
제출기록13680KB / 68ms
최고기록68ms
해결날짜2023/07/21

풀이

코드

"""Solution code for "BOJ 23066. Stones".

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

Tags: [game theory]
"""

import sys


def main():
    N = int(sys.stdin.readline())
    a = [int(x) for x in sys.stdin.readline().split()]

    single_stone_piles = {i for i, a_i in enumerate(a) if a_i == 1}
    multiple_stone_pile_count = N - len(single_stone_piles)
    while True:
        k = int(sys.stdin.readline()) - 1

        if a[k] > 1:
            should_take_all = (multiple_stone_pile_count == 1) == (
                len(single_stone_piles) % 2 == 0
            )
            if should_take_all:
                print(a[k])
            else:
                print(a[k] - 1)
                single_stone_piles.add(k)
            multiple_stone_pile_count -= 1
        else:
            print(a[k])
            single_stone_piles.discard(k)

        if single_stone_piles:
            print(single_stone_piles.pop() + 1, flush=True)
        else:
            print('-1', flush=True)
            break

        y = int(sys.stdin.readline())


if __name__ == '__main__':
    main()