본문 바로가기

코딩 테스트/정렬11

카드 정렬하기 p.363 import sys import heapq input=sys.stdin.readline N=int(input()) heap=[] for i in range(N): data=int(input()) heapq.heappush(heap,data) result = 0 # 힙에 원소가 1개 남을 때까지 while len(heap)!=1: one=heapq.heappop(heap) two=heapq.heappop(heap) sum_value=one+two result+=sum_value heapq.heappush(heap, sum_value) print(result) * heapq는 넣기만 해도 정렬된다는 것을 이용 * deque나 stack으로 한 후 sort하는 것은 시간 초과가 남 2021. 2. 15.
실패율 p.361 방법 1 def solution(N, stages): stop = [0] * (max(stages)+1) stop_ratio = [] people = len(stages) result=[] for i in range(people): stop[stages[i]] += 1 for i in range(1, N+1): if people==0: stop_ratio.append((i,0)) continue ratio=stop[i]/people people -= stop[i] stop_ratio.append((i, ratio)) stop_ratio.sort(key=lambda x: (-x[1])) for i in range(len(stop_ratio)): result.append(stop_ratio[i][.. 2021. 2. 15.
안테나 p. 360 import sys input=sys.stdin.readline n=int(input()) place=list(map(int, input().split())) place.sort() print(place[(n-1)//2]) * print시 그냥 n//2를 하면 안됨 * n은 집의 갯수를 나타내고 place는 리스트이기 때문에 0부터 시작 2021. 2. 15.
국영수 p.359 import sys input=sys.stdin.readline n=int(input()) info=[] for i in range(n): info.append(input().split()) info.sort(key=lambda x:(-int(x[1]), int(x[2]), -int(x[3]), x[0])) for i in range(n): print(info[i][0]) 2021. 2. 12.
두 배열의 원소 교체 p.182 import sys input=sys.stdin.readline n,k=map(int, input().split()) a=list(map(int, input().split())) b=list(map(int, input().split())) a.sort() b.sort(reverse=True) for i in range(k): if a[i] 2021. 2. 11.
성적이 낮은 순서로 학생 출력하기 p.180 import sys input=sys.stdin.readline N=int(input()) info=[] for i in range(N): info_data=input().split() info.append((info_data[0],int(info_data[1]))) info.sort(key=lambda x:x[1]) for i in range(N): print(info[i][0], end=' ') 2021. 2. 11.