Dizzy999
dizzy-theme
Dizzy999
전체 방문자
오늘
어제
  • 분류 전체보기 (34)
    • Machine Learning (1)
      • 논문 리뷰 (0)
      • 수학통계 이론 (1)
      • 짧은글 (0)
    • 알고리즘 (22)
      • 알고리즘 이론 (1)
      • 알고리즘 문제 (20)
      • 짧은글 (1)
    • 프로그램 (2)
      • 프로그래밍 용어 및 상식 (1)
      • C++ (1)
    • 미니프로젝트 (0)
      • Moon Tracker (0)
    • Theme (0)
      • 짧은글 (0)
    • 아두이노&라즈베리파이 (1)
      • 아두이노 (0)
      • 라즈베리파이 (1)
    • Miscellaneous (3)
      • 이과가 이해한 Color (3)
    • OpenCV (0)
      • Tutorial (0)
    • Manim (5)
      • Tutorial (5)

블로그 메뉴

  • 홈
  • 방명록
  • 태그

공지사항

인기 글

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Dizzy999

dizzy-theme

알고리즘/알고리즘 문제

[백준-1655] 가운데를 말해요 - Python

2021. 8. 31. 14:57

 

 

문제 : [백준-1655] 가운데를 말해요 (Python)

출처 :  백준

 

 


   사용한 알고리즘  

더보기

자료 구조 : heapq 를 이용한 탐색



   Python Code    

import sys
import heapq


sys.stdin = open("input.txt", "r")

N = int(input())

right_heap=[] # 최소 힙
left_heap=[]  # 최대 힙

for _ in range(N):
    temp_input = int(input())

    if len(left_heap)==len(right_heap):
        heapq.heappush(left_heap,(-temp_input,temp_input))
    else :
        heapq.heappush(right_heap, (temp_input, temp_input))

    if right_heap and right_heap[0][1]<left_heap[0][1]:
        left_value = heapq.heappop(left_heap)[1]
        right_value = heapq.heappop(right_heap)[1]
        heapq.heappush(right_heap,(left_value,left_value))
        heapq.heappush(left_heap, (-right_value, right_value))

    print(left_heap[0][1])

 

 

 

 

 

반응형

'알고리즘 > 알고리즘 문제' 카테고리의 다른 글

[백준-2573] 빙산 - Python  (0) 2021.11.03
[백준-5014] 스타트링크 - Python  (0) 2021.10.15
다익스트라 알고리즘  (0) 2021.09.11
두 수의 합  (0) 2021.09.07
[백준 - 12865] 평범한 배낭 - Python  (0) 2021.08.29
    '알고리즘/알고리즘 문제' 카테고리의 다른 글
    • [백준-5014] 스타트링크 - Python
    • 다익스트라 알고리즘
    • 두 수의 합
    • [백준 - 12865] 평범한 배낭 - Python
    Dizzy999
    Dizzy999

    티스토리툴바