업다운 파이썬

November·2024년 10월 5일
def remaining_participants(n, k, participants, queries):
    # queries: (x, c)의 리스트. c=1이면 업, c=0이면 다운
    for x, c in queries:
        if c == 1:
            # 업일 때: 선택한 수가 x보다 큰 사람만 살아남는다.
            participants = [p for p in participants if p > x]
        else:
            # 다운일 때: 선택한 수가 x보다 작은 사람만 살아남는다.
            participants = [p for p in participants if p < x]
        
        # 모든 참가자가 탈락하면 반복을 종료
        if not participants:
            break
    
    return len(participants)

# 입력 받기
n, k = map(int, input().split())  # 첫 번째 줄: n, k
participants = list(map(int, input().split()))  # 두 번째 줄: 참가자가 선택한 수들
queries = [tuple(map(int, input().split())) for _ in range(k)]  # 세 번째 줄부터 k개의 줄에 x, c

# 결과 출력
print(remaining_participants(n, k, participants, queries))

k개의 줄에 걸쳐서 x,c 입력 받기

n,k =map(int,input().split())
num_list=list(map(int,input().split()))
request=[tuple(map(int,input().split())) for _ in range(k)]
def remaining(n,k,num_list,request):
	for x,c in request:
    	if c==1:
        	num_list=[p for p in num_list if p>x]
        else:
        	num_list=[p for p in num_list if p<x]
        if not num_list: 
        	break
    return len(num_list)

리스트 컴프리헨션

num_list=[]
for i in range(1,11):
	num_list.append(i)
num_list=[x for x in range(11)]

0개의 댓글