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

3-H0ngJu #156

Merged
merged 2 commits into from
Mar 25, 2024
Merged

3-H0ngJu #156

merged 2 commits into from
Mar 25, 2024

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented Mar 10, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

N๋ฒˆ์งธ ํฐ ์ˆ˜

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

30-40๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์ด ๋ฌธ์ œ๋Š” ๋ฐฐ์—ด์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์ •๋ ฌํ•ด์„œ arr[line] ํ˜น์€ arr[len(arr)-line]์œผ๋กœ ๋‹ต์€ ๊ตฌํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๋กœ ํ†ต๊ณผ๋Š” ์•ˆ๋ฉ๋‹ˆ๋‹ค..!

๊ทธ๋ž˜์„œ heap์„ ํ†ตํ•ด ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.

  1. heap์˜ ํฌ๊ธฐ๊ฐ€ line ๋ฏธ๋งŒ์ด๋ฉด push
  2. heap์˜ ํฌ๊ธฐ๊ฐ€ line ์ด์ƒ์ด๋ฉด, ์ตœ์†Ÿ๊ฐ’๊ณผ ๋น„๊ตํ•ด์„œ ํฌ๋ฉด popํ•˜๊ณ  ์ƒˆ ์›์†Œ๋ฅผ push

์ฒ˜์Œ์—๋Š” ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด, ํž™์œผ๋กœ ํ’€์—ˆ๋Š”๋ฐ๋„ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ์Šต๋‹ˆ๋‹ค ใ…œ

import sys
import heapq

line = int(sys.stdin.readline().strip())

array = [list(map(int, sys.stdin.readline().strip().split())) for _ in range(line)]

heap = []
heapq.heapify(heap)

for i in range(len(array)):
    for k in range(len(array[i])):
        if len(heap) < line:
            heapq.heappush(heap, array[i][k])
        else:
            if array[i][k] > heap[0]:
                heapq.heappop(heap)
                heapq.heappush(heap, array[i][k])

print(heap[0])

๊ทธ๋ž˜์„œ ๋ชจ๋“  ์›์†Œ(line*line)๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด array์˜ ๋ฌธ์ œ๋ผ๊ณ  ๋ณด๊ณ , ๋ฐฐ์—ด์˜ ์ €์žฅ์€ ํ•œ ์ค„์”ฉ๋งŒ ํ•˜๋„๋ก for๋ฌธ ์•ˆ์œผ๋กœ ๋„ฃ์—ˆ๋”๋‹ˆ ํ†ต๊ณผ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค -!

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

  • heap์ด๋ผ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ
  1. ํ + ์šฐ์„ ์ˆœ์œ„ = heap (์ด์ง„ ํŠธ๋ฆฌ ๊ตฌ์กฐ)
  2. ์ตœ๋Œ€ํž™, ์ตœ์†Œํž™์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๊ณ , ์ตœ๋Œ€ํž™์€ ์ตœ์†Œํž™์—์„œ -๋ฅผ ๋ถ™์—ฌ ๊ตฌํ˜„
  3. ๋ถ€๋ชจ ๋…ธ๋“œ์™€ ์ž์‹ ๋…ธ๋“œ์˜ ๊ด€๊ณ„
    a. ์™ผ์ชฝ ์ž์‹ = (๋ถ€๋ชจ index) * 2
    b. ์˜ค๋ฅธ์ชฝ ์ž์‹ = (๋ถ€๋ชจ index) * 2 + 1
    c. ๋ถ€๋ชจ index = (์ž์‹ index) / 2

Copy link
Member

@tgyuuAn tgyuuAn 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

def input(): return sys.stdin.readline().rstrip()

N = int(input())

numbers = []
for _ in range(N):
    numbers.extend(map(int,input().split()))
    
print(sorted(numbers, reverse = True)[N-1])

์—ญ์‹œ๋‚˜ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋‚˜๋”๊ตฐ์š”.







225๋งŒ๊ฐœ์˜ ์ •์ˆ˜ํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ์ €์žฅํ•˜๊ธฐ์—๋Š” 12MB๋กœ๋Š” ํ„ฐ๋ฌด๋‹ˆ๊ฐ€ ์—†๊ธฐ ๋–„๋ฌธ์ด์ฃ ...

intํ˜•์ด ๋Œ€๋ถ€๋ถ„ 4๋ฐ”์ดํŠธ์ธ๋ฐ ํŒŒ์ด์ฌ์€ ๋” ๋งŽ์€ ๋ฐ”์ดํŠธ๋ฅผ ์†Œ๋ชจํ•˜๋‹ˆ๊นŒ.. ๋Œ€์ถฉ ๊ณ„์‹ผํ•ด๋ณด๋ฉด ์ดˆ๊ณผ๊ฐ€ ๋‚œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ์–ด์š”.







๊ทธ๋Ÿผ ๋„๋Œ€์ฒด ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ์ข‹์„๊นŒ ๊ณฐ๊ณฐํžˆ ์ƒ๊ฐํ•ด๋ดค๋Š”๋ฐ,

๊ฒฐ๊ตญ์—๋Š” N๋ฒˆ์งธ ํฐ ์ˆ˜๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ N๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋งŒ ์ €์žฅํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์–ด์š”.

์ตœ๊ทผ์— ํ™์ฃผ๋‹˜์ด ์ œ์ถœํ•˜์…จ๋˜ deque๋กœ ์ด์šฉํ–ˆ๋˜ ๋ฒ„ํผ(?) ํ˜น์€ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ๋ฐฉ์‹์„ ๋„์ž…ํ•˜๋˜,

์–ด๋–ค ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ๊ฐ€์žฅ ์ ์ ˆํ• ๊นŒ? ๋ฅผ ๊ณ ๋ฏผํ•ด๋ณด๋‹ˆ๊นŒ,

heap์ด ๋ฐ”๋กœ ๋– ์˜ฌ๋ผ์„œ heap์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋”๋‹ˆ ๋ฐ”๋กœ ํ†ต๊ณผ๋˜์—ˆ์”๋‹ˆ๋‹ค!

import sys
from heapq import *

def input(): return sys.stdin.readline().rstrip()

N = int(input())

board = []
for _ in range(N):
    temp = list(map(lambda x : -1*int(x),input().split()))
    heapify(temp)
    
    # board๊ฐ€ ๋น„์–ด์žˆ์„ ๊ฒฝ์šฐ
    if not board: 
        board.extend(map(lambda x : -1*x, temp))
        heapify(board)
    
    else: 
        while -1*temp[0] > board[0]:
            heappop(board)
            heappush(board, -1*heappop(temp))
            
    # print(board)
            
print(board[0])

Comment on lines +9 to +17
for i in range(line):
data = list(map(int, sys.stdin.readline().strip().split()))
for s in data:
if len(heap) < line:
heapq.heappush(heap, s)
else:
if s > heap[0]:
heapq.heappop(heap)
heapq.heappush(heap, s)
Copy link
Member

Choose a reason for hiding this comment

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

์ง€๊ธˆ์€ ๋ญ ํ”„๋กœ์ ํŠธ๋„ ์•„๋‹ˆ์ง€๋งŒ i๋ผ๋Š” ๋ณ€์ˆ˜๋ช…ํ•˜๊ณ  s๋ผ๋Š” ๋ณ€์ˆ˜๋ช…์€ ์ง€์–‘ํ•˜๋Š” ๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!




์ฝ”๋“œ ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ›๋Š” ์‹œ์Šคํ…œ์ด๋‹ค ๋ณด๋‹ˆ๊นŒ...

PSํ•  ๋•Œ ํด๋ฆฐ ์ฝ”๋“œ๋Š” ์˜ค๋ฒ„ ์—”์ง€๋‹ˆ์–ด๋ง์ด๊ธด ํ•˜์ง€๋งŒ,

์š”์ฆ˜์€ ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง€๋Š” ๊ตฌํ˜„ ๋ฌธ์ œ๋„ ๋งŽ์ด ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๋„ˆ๋ฌด ๋งŽ์€ ์‹œ๊ฐ„์„ ๋“ค์ด์ง€๋Š” ๋ง๊ณ  ์ตœ์†Œํ•œ์˜ ๋…ธ๋ ฅ(?) ์ •๋„๋Š” ํ•ด์ฃผ๋ฉด ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค!




๊ทธ๋ฆฌ๊ณ  ์ง€๊ธˆ์€ ์‹œ๊ฐ„ ์ œํ•œ์ด ์žˆ๋Š” ์‹œํ—˜๋„ ์•„๋‹ˆ๊ธฐ์— ์ด๋Ÿฐ ํด๋ฆฐ ์ฝ”๋“œ๋ฅผ ์ฒดํ™” ์‹œํ‚ค๊ธฐ์—๋Š” ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋˜์–ด์„œ์š”!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

์˜ค์˜ค ์ข‹์€ ๋ฆฌ๋ทฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค (_ _)

์•„๋งˆ ์ € ๋‹น์‹œ์—๋Š” i๋Š” ๋‹จ์ˆœ ๋ฐ˜๋ณต๋ฌธ์„ ์œ„ํ•œ ๋ณ€์ˆ˜, s๋Š” ๋‹จ์ˆœํžˆ ์›์†Œ๋ฅผ data์—์„œ ๊บผ๋‚ด์˜จ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ ์ผ๋˜ ๊ฒƒ ๊ฐ™๋„ค์šฉ

ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ˜์˜ํ•˜๋ฉด, for _ in range(line):๊ณผ for num in data:๋กœ ๊ฐ๊ฐ i์™€ s๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”!

line = int(sys.stdin.readline().strip())

heap = []
heapq.heapify(heap)
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ด ํ•จ์ˆ˜๋Š” ์—ฌ๊ธฐ์—์„œ ํ˜ธ์ถœ์•ˆํ•˜์…”๋„ ๋ผ์š”
heap ๋ฆฌ์ŠคํŠธ ์›์†Œ ํŠธ๋ฆฌ๋ฅผ ์žฌ ๊ตฌ์„ฑํ•˜๋Š”๊ฑฐ๋ผ ๋น„์–ด์žˆ์„ ๋•Œ๋Š” ๋ถˆํ•„์š”ํ•œ ํ˜ธ์ถœ์ด๋ž๋‹ˆ๋‹ค

@pknujsp
Copy link
Collaborator

pknujsp commented Mar 16, 2024

ํ•œ๋‹ฌ ์ „์— ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๋•Œ๋ฌธ์— ํ‘ธ๋Š” ๋ฐ ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ ธ๋˜ ๋ฌธ์ œ๋„ค์š”.

์ฒ˜์Œ์€ ๋ฐ‘์— ์ฒ˜๋Ÿผ ๋ฆฌ์ŠคํŠธ์— ์›์†Œ๋ฅผ ์ „๋ถ€๋‹ค ๋„ฃ๊ณ 
heapify() ๋กœ ์ตœ๋Œ€ ํž™์„ ๋งŒ๋“  ํ›„
N - 1๋ฒˆ ์ตœ๋Œ“๊ฐ’์„ ๋บธ ๋’ค์—, ๊ทธ ๋‹ค์Œ ์ตœ๋Œ“๊ฐ’์„ ์ถœ๋ ฅํ•˜๋„๋ก ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜๋‹ˆ๊นŒ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ„ฐ์ง€๋”๋ผ๊ตฌ์š”.
์ด ๋ถ€๋ถ„์€ ํ™์ฃผ๋‹˜์ด ์จ๋†“์œผ์‹  ๋‚ด์šฉ์ด๊ตฐ์š”

for _ in range(N):
    heap.extend(map(lambda v: -int(v), stdin.readline().split()))
heapify(heap)

for _ in range(N - 1):
    heappop(heap)

print(-heappop(heap))

๋‹จ์ˆœ ์ •๋ ฌ์„ ํ•˜๋Š” ๊ฒƒ ๋ฟ์ธ๋ฐ ๋‹ค์‹œ ๋ณด๋‹ˆ ์ •๋ง ์ด์ƒํ•œ ์ฝ”๋“œ๋„ค์š”
๋˜‘๊ฐ™์ด ๋ฉ”๋ชจ๋ฆฌ ๋ฌธ์ œ๊ฐ€ ์žˆ์ง€๋งŒ
๋ฆฌ์ŠคํŠธ ์ •๋ ฌํ•˜๊ณ  N๋ฒˆ์งธ ์ธ๋ฑ์Šค ๊ฐ’์„ ๋ณด์—ฌ์ฃผ๊ฒŒ ํ•˜๋Š”๊ฒŒ ๋งž๋Š” ๋ฐฉ์‹์ธ๋ฐ.

์–ด๋–ป๊ฒŒ ๊ณ ์ณ์•ผํ•˜๋‚˜ ๊ณ ๋ฏผํ•ด๋ณด๋‹ˆ

์ „๋ถ€๋‹ค ๋ฆฌ์ŠคํŠธ์— ๋„ฃ๊ณ  ์ตœ๋Œ€ ํž™์œผ๋กœ ๋งŒ๋“ค ํ•„์š”์—†์ด
์ตœ์†Œ ํž™์„ ์‚ฌ์šฉํ•˜๊ณ  ํฌ๊ธฐ๋ฅผ N์œผ๋กœ ์œ ์ง€์‹œํ‚ค๋ฉด ๋˜๋”๋ผ๊ตฌ์š”.

์˜ˆ๋ฅผ ๋“ค์–ด [7, 4, 3, 9, 1, 2]์—์„œ 3๋ฒˆ์งธ ํฐ ์ˆ˜๋ฅผ ์ฐพ๋Š”๋‹ค๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ดค์„๋•Œ
์ตœ์†Œ ํž™ ๋ฆฌ์ŠคํŠธ X๋ฅผ ๋งŒ๋“ค๊ณ , ์›์†Œ๋“ค์„ ์ˆœ์„œ๋Œ€๋กœ ์ˆœํšŒํ•˜๋ฉด์„œ
๊ฒฝ์šฐ์— ๋”ฐ๋ผ ์ถ”๊ฐ€ํ•ด๊ฐ€๋ฉด 3๋ฒˆ์งธ ํฐ ์ˆ˜๊ฐ€ ํž™์˜ ์ตœ์†Ÿ๊ฐ’์ด ๋œ๋‹ค๋Š” ๊ฑธ ์•Œ๊ฒŒ๋ฌ์Šต๋‹ˆ๋‹ค.

  • X : [7, 4, 3] : Xํฌ๊ธฐ๊ฐ€ 3๋ณด๋‹ค ์ž‘์œผ๋‹ˆ, 7, 4, 3์„ ์ˆœ์„œ๋Œ€๋กœ ์ถ”๊ฐ€
  • [7, 4, 9] : 3์„ ๋นผ๊ณ , 9๋ฅผ ์ถ”๊ฐ€, 1, 2๋Š” 3๋ฒˆ์งธ ํฐ ์ˆ˜๊ฐ€ ๋  ์ˆ˜ ์—†์œผ๋‹ˆ ์ถ”๊ฐ€X
  • [7, 4, 9] : ์ตœ์†Ÿ๊ฐ’์ธ 4๊ฐ€ 3๋ฒˆ์งธ ํฐ ์ˆ˜

์ข€ ๊ธธ์–ด์ง€๊ธด ํ–ˆ๋Š”๋ฐ
์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๋ฒ•์„ ์ข€ ๋” ๋ฐฐ์šธ์ˆ˜ ์žˆ์—ˆ๋˜ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค

์ œ ์ฝ”๋“œ๊ฐ€ ํ™์ฃผ๋‹˜ ์ฝ”๋“œ๋ž‘ ๊ฑฐ์˜ ๋˜‘๊ฐ™๋„ค์š”

N = int(stdin.readline())
heap = []

for _ in range(N):
    for v in map(int, stdin.readline().split()):
        if len(heap) < N:
            heappush(heap, v)
        elif v >= heap[0]:
            heappush(heap, v)
            heappop(heap)

print(heap[0])

@Munbin-Lee
Copy link
Member

Munbin-Lee commented Mar 24, 2024

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    cin >> N;

    vector<int> v(N * N);

    for (int &num: v) {
        cin >> num;
    }

    sort(v.rbegin(), v.rend());

    cout << v[N - 1];

    return 0;
}

๋ ์š”์˜น C++๋Š” ๋˜๋„ค์š”

image

@Munbin-Lee
Copy link
Member

Munbin-Lee commented Mar 24, 2024

#include <iostream>
#include <queue>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    cin >> N;

    priority_queue<int, vector<int>, greater<>> pq;

    for (int i = 0; i < N; i++) {
        int num;
        cin >> num;
        pq.emplace(num);
    }

    for (int i = 0; i < N * (N - 1); i++) {
        int num;
        cin >> num;
        pq.emplace(num);
        pq.pop();
    }

    cout << pq.top();

    return 0;
}

์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋ฆฌ๋ทฐ๋กœ ๋‹ฌ์•„๋†“๊ฒ ์Šต๋‹ˆ๋‹ค.

image

Comment on lines +15 to +17
if s > heap[0]:
heapq.heappop(heap)
heapq.heappush(heap, s)
Copy link
Member

Choose a reason for hiding this comment

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

๊ฒฐ๊ตญ ํž™ ์ž์ฒด๊ฐ€ ํฌ๊ธฐ ๋น„๊ต๋ฅผ ์œ„ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ์ด๊ธฐ ๋•Œ๋ฌธ์—, ์กฐ๊ฑด๋ฌธ์„ ๋‹ฌ์•„๋†“์„ ํ•„์š” ์—†์ด ๋ฐ”๋กœ push ํ›„ popํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค!

heapq.heappush(heap, s)
heapq.heappop(heap)

heappushpop()์„ ์‚ฌ์šฉํ•˜๋ฉด ํ•œ ์ค„๋กœ๋„ ๋˜๊ตฌ์š”.

@H0ngJu H0ngJu merged commit 301cbcc into main Mar 25, 2024
@H0ngJu H0ngJu deleted the 3-H0ngJu branch March 25, 2024 14:38
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.

4 participants