728x90

 

원래 문제는 다음과 같습니다.

 

생각보다 순서대로 분기를 잘 나눠주면서 하면 될 것 같습니다.

from collections import defaultdict
import sys
import heapq
input = sys.stdin.readline

score_dae, score_gyu = 0,0
num_dae, num_gyu = defaultdict(int), defaultdict(int)
third_prime_dae, third_prime_gyu = [-1,-1,-1], [-1,-1,-1]


prime = defaultdict(int)
num = [False for _ in range (0,5000001)]
num[0], num[1] = True, True
for i in range (2,5000001):
    if num[i] == False:
        prime[i] = 1
        for j in range (2 * i, 5000001, i):
            num[j] = True
#prime에 소수 저장 완료

def check(n,i):
    global third_prime_gyu
    global third_prime_dae
    global score_gyu
    global score_dae
    if prime[n] == 0: #소수가 아닌 경우 페널티를 주는 방향으로
        if i == -1: #-1를 대웅이의 턴
            x = heapq.heappop(third_prime_gyu)
            if x == -1:
                score_gyu += 1000
                heapq.heappush(third_prime_gyu, -1)
            else:
                score_gyu += x
                heapq.heappush(third_prime_gyu, x)
        elif i == 1: #1을 규성이의 턴
            x = heapq.heappop(third_prime_dae)
            if x == -1:
                score_dae += 1000
                heapq.heappush(third_prime_dae, -1)
            else:
                score_dae += x
                heapq.heappush(third_prime_dae, x)
    else: #일단 소수를 말했는가?
        if num_dae[n] != 0 or num_gyu[n] != 0: #이전에 말해진 소수인 경우
            if i == -1:
                score_dae -= 1000
            elif i == 1:
                score_gyu -= 1000
        elif num_dae[n] == 0 and num_gyu[n] == 0: #이전에 나오지 않은 수
            if i == -1:
                num_dae[n] = 1
                heapq.heappush(third_prime_dae,n)
                heapq.heappop(third_prime_dae)
            elif i == 1:
                num_gyu[n] = 1
                heapq.heappush(third_prime_gyu, n)
                heapq.heappop(third_prime_gyu)
N = int(input())
for _ in range (0,N):
    a,b = map(int,input().split())
    check(a,-1)
    check(b,1)

if score_dae > score_gyu:
    print("소수의 신 갓대웅")
elif score_dae < score_gyu:
    print("소수 마스터 갓규성")
elif score_dae == score_gyu:
    print("우열을 가릴 수 없음")

+ Recent posts