Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15-wkdghdwns199 #63

Merged
merged 6 commits into from
Mar 27, 2024
Merged

15-wkdghdwns199 #63

merged 6 commits into from
Mar 27, 2024

Conversation

wkdghdwns199
Copy link
Collaborator

πŸ”— 문제 링크

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

문제
μ ˆλŒ“κ°’ νž™μ€ λ‹€μŒκ³Ό 같은 연산을 μ§€μ›ν•˜λŠ” μžλ£Œκ΅¬μ‘°μ΄λ‹€.

배열에 μ •μˆ˜ x (x β‰  0)λ₯Ό λ„£λŠ”λ‹€.
λ°°μ—΄μ—μ„œ μ ˆλŒ“κ°’μ΄ κ°€μž₯ μž‘μ€ 값을 좜λ ₯ν•˜κ³ , κ·Έ 값을 λ°°μ—΄μ—μ„œ μ œκ±°ν•œλ‹€. μ ˆλŒ“κ°’μ΄ κ°€μž₯ μž‘μ€ 값이 μ—¬λŸ¬κ°œμΌ λ•ŒλŠ”, κ°€μž₯ μž‘μ€ 수λ₯Ό 좜λ ₯ν•˜κ³ , κ·Έ 값을 λ°°μ—΄μ—μ„œ μ œκ±°ν•œλ‹€.
ν”„λ‘œκ·Έλž¨μ€ μ²˜μŒμ— λΉ„μ–΄μžˆλŠ” λ°°μ—΄μ—μ„œ μ‹œμž‘ν•˜κ²Œ λœλ‹€.

μž…λ ₯
첫째 쀄에 μ—°μ‚°μ˜ 개수 N(1≀N≀100,000)이 주어진닀. λ‹€μŒ N개의 μ€„μ—λŠ” 연산에 λŒ€ν•œ 정보λ₯Ό λ‚˜νƒ€λ‚΄λŠ” μ •μˆ˜ xκ°€ 주어진닀. λ§Œμ•½ xκ°€ 0이 μ•„λ‹ˆλΌλ©΄ 배열에 xλΌλŠ” 값을 λ„£λŠ”(μΆ”κ°€ν•˜λŠ”) 연산이고, xκ°€ 0이라면 λ°°μ—΄μ—μ„œ μ ˆλŒ“κ°’μ΄ κ°€μž₯ μž‘μ€ 값을 좜λ ₯ν•˜κ³  κ·Έ 값을 λ°°μ—΄μ—μ„œ μ œκ±°ν•˜λŠ” κ²½μš°μ΄λ‹€. μž…λ ₯λ˜λŠ” μ •μˆ˜λŠ” -231보닀 크고, 231보닀 μž‘λ‹€.

좜λ ₯
μž…λ ₯μ—μ„œ 0이 주어진 회수만큼 닡을 좜λ ₯ν•œλ‹€. λ§Œμ•½ 배열이 λΉ„μ–΄ μžˆλŠ” 경우인데 μ ˆλŒ“κ°’μ΄ κ°€μž₯ μž‘μ€ 값을 좜λ ₯ν•˜λΌκ³  ν•œ κ²½μš°μ—λŠ” 0을 좜λ ₯ν•˜λ©΄ λœλ‹€.

예제 μž…λ ₯ 1
18
1
-1
0
0
0
1
1
-1
-1
2
-2
0
0
0
0
0
0
0

예제 좜λ ₯ 1
-1
1
0
-1
-1
1
1
-2
2
0

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

30λΆ„ - μš°μ„ μˆœμœ„ 큐 λ‹€μ‹œ ν•œ 번 μ•Œμ•„λ³΄μž!

✨ μˆ˜λ„ μ½”λ“œ

값을 계속 μž…λ ₯λ°›λ‹€κ°€ 0 이 μž…λ ₯되면 받은 숫자 쀑 κ°€μž₯ μ ˆλŒ“κ°’μ΄ 큰 수λ₯Ό 좜λ ₯ν•΄μ•Ό ν•˜κ³ , λ§Œμ•½ μ ˆλŒ“κ°’μ΄ κ°™μœΌλ©΄ λ¨Όμ € λ“€μ–΄μ˜¨ 숫자λ₯Ό 좜λ ₯ν•΄μ•Ό ν•˜λŠ” λ¬Έμ œμž…λ‹ˆλ‹€!

μš°μ„ μˆœμœ„ 큐λ₯Ό μ΄μš©ν•˜μ—¬ 제일 큰 μ ˆλŒ“κ°’μΈ 수λ₯Ό 맨 μ•žμœΌλ‘œ 가져와야 ν•©λ‹ˆλ‹€. μŒμˆ˜λ„ μžˆμœΌλ―€λ‘œ 음수λ₯Ό 담을 μš°μ„ μˆœμœ„ 큐, μ–‘μˆ˜λ₯Ό 담을 μš°μ„ μˆœμœ„ 큐λ₯Ό 각각 λ§Œλ“€μ–΄μ€λ‹ˆλ‹€.

μŒμˆ˜λŠ” 음수 μš°μ„ μˆœμœ„ 큐, μ–‘μˆ˜λŠ” μ–‘μˆ˜ μš°μ„ μˆœμœ„ 큐에 λ‹΄λ‹€κ°€, λ§Œμ•½ 0 이 μž…λ ₯λœλ‹€λ©΄ 확인을 μ‹œμž‘ν•©λ‹ˆλ‹€.

음수 μš°μ„ μˆœμœ„ 큐에 데이터가 있고, μ–‘μˆ˜μ—λ„ 있으면 맨 μ•žμ— μžˆλŠ” 각 μš°μ„ μˆœμœ„ 큐의 μ΅œλŒ€ 수 쀑 더 큰 수λ₯Ό pop ν•΄μ„œ 좜λ ₯ν•΄μ€λ‹ˆλ‹€.
λ§Œμ•½ μš°μ„ μˆœμœ„ νμ—λ§Œ 데이터가 μžˆκ±°λ‚˜ μ–‘μˆ˜μ—λ§Œ 데이터가 있으면 κ·Έ μͺ½ μ΅œλŒ€ 수λ₯Ό pop ν•΄μ„œ 좜λ ₯ν•΄μ€λ‹ˆλ‹€.
λ§Œμ•½ λ‘˜ λ‹€ λΉ„μ–΄μžˆμœΌλ©΄ 0을 좜λ ₯ν•΄μ€λ‹ˆλ‹€.

μš°μ„  μˆœμœ„ 큐λ₯Ό ν™œμš©ν•΄λ³Ό 수 μžˆλŠ” μ•„μ£Ό 쒋은 λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€.

import sys, heapq
input =sys.stdin.readline
min_heap = []
max_heap = []
N = int(input())
for _ in range(N):
    number = int(input())
    if (number > 0) :
        heapq.heappush(min_heap, number)
    elif (number < 0) :
        heapq.heappush(max_heap, -number)
    else :
        if min_heap :
            if max_heap :
                if min_heap[0] < max_heap[0] :
                    print(heapq.heappop(min_heap))
                else :
                    print(-heapq.heappop(max_heap))
            else :
                print(heapq.heappop(min_heap))
        elif max_heap :
            print(-heapq.heappop(max_heap))
        else :
            print(0)

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

Copy link
Member

@fnzksxl fnzksxl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음.. 이 문제.. μ΄λ ‡κ²Œ 풀어도 λ©λ‹ˆλ‹€!
νŠœν”Œμ˜ 첫 번째, 두 번째 인자 순으둜 정렬이 되기 λ•Œλ¬Έμ—
(μ ˆλŒ“κ°’, μ‹€μ œ κ°’)을 λ„£μ–΄λ†“μœΌλ©΄ μ ˆλŒ“κ°’μ΄ μž‘μ€ μˆœμ„œ -> μ‹€μ œ μˆ«μžκ°€ μž‘μ€ μˆœμ„œλ‘œ
정렬이 λ©λ‹ˆλ‹€!

import sys
from heapq import *

input = sys.stdin.readline

N = int(input())
q = []

for _ in range(N):
    command = int(input())
    
    if command:
        heappush(q, (abs(command), command))
    else:
        if q:
            _, num = heappop(q)
            print(num)
        else:
            print(0)

@alstjr7437 alstjr7437 removed their request for review March 23, 2024 13:33
@wkdghdwns199 wkdghdwns199 marked this pull request as ready for review March 25, 2024 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants