4W.6D -정렬 summary

Dazz_heyDay ·2021년 7월 25일
1

Python) Algorithm_study

목록 보기
23/39

✏️ 문제 [통계학]

https://www.acmicpc.net/problem/2108

import sys
from collections import Counter
n = int(sys.stdin.readline())
N = []
for i in range(n):
    N.append(int(sys.stdin.readline()))
N.sort()
nums= Counter(N).most_common()
print(round(sum(N)/ n))
print(N [n // 2])
if len(nums) > 1:
    if nums_s[0][1] == nums[1][1]:
        print(nums[1][0])
    else:
        print(nums[0][0])
else:
    print(nums[0][0])
print(N[-1] -N[0])

✏️문제[카드 정렬하기]

import heapq
import sys
input=lambda:sys.stdin.readline.rstrip()
card=[]
n=int(input())
for_in range(n):
	heapq.heappush(card,int(input()))
if n==1:
	print(0)
else:
	sum=0
    while len(card)>1:
    	tmp=heapq.heappop(card)+heapq.heappop(card)
        heapq.heappush(card,tmp)
        sum+=tmp
    print(sum)

파이썬 힙큐 (heapq)

힙에 원소 추가: heapq.heappush(a,b)

a=원소 추가 대상 리스트
b=추가할 원소

heapq.heappush(heap,7)
heapq.heappush(heap,4)
heapq.heappush(heap,9)
heapq.heappush(heap,3)
print(heap)
->[3,4,7,9]

힙에서 원소 삭제:heapq.heappop(a))

a=원소를 삭제할 대상 리스트->가장 작은 원소 삭제 후 값 리턴

print(heapq.heappop(heap))
print(heap)
->
4
[3,7,9]

참고 블로그:https://www.daleseo.com/python-heapq/

profile
Why.Not.Now

2개의 댓글

comment-user-thumbnail
2021년 7월 26일

안녕하세요, 김덕우입니다! 마침 힙을 더 공부해보려고 했는데, 상세히 포스팅해주셔서 배워갈 수 있었습니다. 감사합니다! 이번주도 화이팅입니다!!

답글 달기
comment-user-thumbnail
2021년 7월 26일

안녕하세요 알고리줌입니다.
저도 힙이 생소했는데 정리해주셔서 감사합니다.
정렬주 수고 많으셨습니다. 다음주도 화이팅해요!

답글 달기