[BOJ][๐ŸŸก5][๋ฐฑ์ค€#02470] ๋‘ ์šฉ์•ก

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #2470


๋ฌธ์ œ

KOI ๋ถ€์„ค ๊ณผํ•™์—ฐ๊ตฌ์†Œ์—์„œ๋Š” ๋งŽ์€ ์ข…๋ฅ˜์˜ ์‚ฐ์„ฑ ์šฉ์•ก๊ณผ ์•Œ์นผ๋ฆฌ์„ฑ ์šฉ์•ก์„ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋‹ค. ๊ฐ ์šฉ์•ก์—๋Š” ๊ทธ ์šฉ์•ก์˜ ํŠน์„ฑ์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ•˜๋‚˜์˜ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ ธ์žˆ๋‹ค. ย ์‚ฐ์„ฑ ์šฉ์•ก์˜ ํŠน์„ฑ๊ฐ’์€ 1๋ถ€ํ„ฐ 1,000,000,000๊นŒ์ง€์˜ ์–‘์˜ ์ •์ˆ˜๋กœ ๋‚˜ํƒ€๋‚ด๊ณ , ์•Œ์นผ๋ฆฌ์„ฑ ์šฉ์•ก์˜ ํŠน์„ฑ๊ฐ’์€ -1๋ถ€ํ„ฐ -1,000,000,000๊นŒ์ง€์˜ ์Œ์˜ ์ •์ˆ˜๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๊ฐ™์€ ์–‘์˜ ๋‘ ์šฉ์•ก์„ ํ˜ผํ•ฉํ•œ ์šฉ์•ก์˜ ํŠน์„ฑ๊ฐ’์€ ํ˜ผํ•ฉ์— ์‚ฌ์šฉ๋œ ๊ฐ ์šฉ์•ก์˜ ํŠน์„ฑ๊ฐ’์˜ ํ•ฉ์œผ๋กœ ์ •์˜ํ•œ๋‹ค. ์ด ์—ฐ๊ตฌ์†Œ์—์„œ๋Š” ๊ฐ™์€ ์–‘์˜ ๋‘ ์šฉ์•ก์„ ํ˜ผํ•ฉํ•˜์—ฌ ํŠน์„ฑ๊ฐ’์ด 0์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์šฉ์•ก์„ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค.ย  ์˜ˆ๋ฅผ ๋“ค์–ด, ์ฃผ์–ด์ง„ ์šฉ์•ก๋“ค์˜ ํŠน์„ฑ๊ฐ’์ด [-2, 4, -99, -1, 98]์ธ ๊ฒฝ์šฐ์—๋Š” ํŠน์„ฑ๊ฐ’์ด -99์ธ ์šฉ์•ก๊ณผ ํŠน์„ฑ๊ฐ’์ด 98์ธ ์šฉ์•ก์„ ํ˜ผํ•ฉํ•˜๋ฉด ํŠน์„ฑ๊ฐ’์ด -1์ธ ์šฉ์•ก์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ์ด ์šฉ์•ก์ด ํŠน์„ฑ๊ฐ’์ด 0์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์šฉ์•ก์ด๋‹ค. ์ฐธ๊ณ ๋กœ, ๋‘ ์ข…๋ฅ˜์˜ ์•Œ์นผ๋ฆฌ์„ฑ ์šฉ์•ก๋งŒ์œผ๋กœ๋‚˜ ํ˜น์€ ๋‘ ์ข…๋ฅ˜์˜ ์‚ฐ์„ฑ ์šฉ์•ก๋งŒ์œผ๋กœ ํŠน์„ฑ๊ฐ’์ด 0์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ํ˜ผํ•ฉ ์šฉ์•ก์„ ๋งŒ๋“œ๋Š” ๊ฒฝ์šฐ๋„ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฐ์„ฑ ์šฉ์•ก๊ณผ ์•Œ์นผ๋ฆฌ์„ฑ ์šฉ์•ก์˜ ํŠน์„ฑ๊ฐ’์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด ์ค‘ ๋‘ ๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ์šฉ์•ก์„ ํ˜ผํ•ฉํ•˜์—ฌ ํŠน์„ฑ๊ฐ’์ด 0์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์šฉ์•ก์„ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๋‘ ์šฉ์•ก์„ ์ฐพ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์—๋Š” ์ „์ฒด ์šฉ์•ก์˜ ์ˆ˜ N์ด ์ž…๋ ฅ๋œ๋‹ค. N์€ 2 ์ด์ƒ 100,000 ์ดํ•˜์ด๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์šฉ์•ก์˜ ํŠน์„ฑ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š” N๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. ์ด ์ˆ˜๋“ค์€ ๋ชจ๋‘ -1,000,000,000 ์ด์ƒ 1,000,000,000 ์ดํ•˜์ด๋‹ค. N๊ฐœ์˜ ์šฉ์•ก๋“ค์˜ ํŠน์„ฑ๊ฐ’์€ ๋ชจ๋‘ ๋‹ค๋ฅด๊ณ , ์‚ฐ์„ฑ ์šฉ์•ก๋งŒ์œผ๋กœ๋‚˜ ์•Œ์นผ๋ฆฌ์„ฑ ์šฉ์•ก๋งŒ์œผ๋กœ ์ž…๋ ฅ์ด ์ฃผ์–ด์ง€๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ํŠน์„ฑ๊ฐ’์ด 0์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์šฉ์•ก์„ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๋‘ ์šฉ์•ก์˜ ํŠน์„ฑ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋Š” ๋‘ ์šฉ์•ก์€ ํŠน์„ฑ๊ฐ’์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. ํŠน์„ฑ๊ฐ’์ด 0์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์šฉ์•ก์„ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋‘ ๊ฐœ ์ด์ƒ์ผ ๊ฒฝ์šฐ์—๋Š” ๊ทธ ์ค‘ ์•„๋ฌด๊ฒƒ์ด๋‚˜ ํ•˜๋‚˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์ž…๋ ฅ

5
-2 4 -99 -1 98


์ถœ๋ ฅ

-99 98


My Sol

import sys
input = sys.stdin.readline
from heapq import heappop, heappush

N = int(input().rstrip())
arr = list(map(int, input().rstrip().split()))

maxH = []
minH = []
for a in arr:
    heappush(maxH, -a)
    heappush(minH, a)

diff = float('inf')
mx, mn = -heappop(maxH), heappop(minH)
while mx > mn:
    if diff > abs(mn+mx):
        diff = abs(mn+mx)
        rmn, rmx = mn, mx
    if not diff: break
    if mx+mn < 0:
        mn = heappop(minH)
    else :
        mx = -heappop(maxH)

print(rmn, rmx)

heapq๋ฅผ ์ด์šฉํ•ด ์–‘์ชฝ์—์„œ ์ขํ˜€์˜ค๋ฉฐ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•˜์˜€๋‹ค. heapq๋Š” ์ •๋ ฌ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(nlogn)์ด๋ฏ€๋กœ ํฌ๊ธฐ์ˆœ์œผ๋กœ ์ตœ๋Œ€ํž™๊ณผ ์ตœ์†Œํž™์„ ๊ตฌํ˜„ํ•˜์—ฌ ์–‘์ชฝ์—์„œ ๋น„๊ตํ•˜๋ฉฐ ๊ฐฑ์‹ ํ•˜๋ฉฐ ์ตœ์†Œ ์ฐจ์ด๋ฅผ ๊ธฐ๋กํ•˜์˜€๊ณ , ์ด ๊ฐฑ์‹  ๋•Œ์˜ ์ž‘์€ ๊ฐ’๊ณผ ํฐ ๊ฐ’์„ ๊ธฐ๋กํ•˜์—ฌ ์ถœ๋ ฅํ•˜์˜€๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

import sys

N = int(sys.stdin.readline())
A = sorted(map(int, sys.stdin.readline().split()))

res = 2000000002
start = 0
end = len(A)-1
one = 0
two = 0
while start<end:
    temp = A[start] + A[end]
    val = abs(temp)

    if res>val:
        res = val
        one = A[start]
        two = A[end]

    if temp>0:
        end-=1
    else:
        start+=1
print(one, two)

๋‚ด ์—ฐ์‚ฐ์‹œ๊ฐ„์˜ ์ ˆ๋ฐ˜์— ํ•ด๋‹นํ•˜๋Š” ํ’€์ด๋ฅผ ์ฐพ์•„ ๋ถ„์„ํ•ด๋ณด๋ ค ํ•œ๋‹ค. heapq์— ๋„ฃ์€ ๊ฒƒ์ด ์•„๋‹Œ sorted๋กœ ์ •๋ ฌํ•ด์„œ ์•ž๊ณผ ๋’ค๋ฅผ ๋‚˜๋ˆ„์–ด ์ขํ˜€์™”๋‹ค. ํ‘œ๋ณธ์˜ ํฌ๊ธฐ๊ฐ€ 10๋งŒ์ธ๋ฐ ์ •๋ ฌ์„ ํ•ด๋„ ์‹œ๊ฐ„์ด ํฌ๊ฒŒ ๋Š˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ด์ƒํ•  ๋”ฐ๋ฆ„์ด๋‹ค. ์•„๋งˆ ์ตœ๋Œ€ 10๋งŒ์„ ํ•ด๋‘๊ณ  10๋งŒ๋งŒํผํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์ผ ๊ฒƒ์ด๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ