[BOJ] #2470 ๋‘ ์šฉ์•ก (Python)

    ๋ฐ˜์‘ํ˜•

    ๐ŸŒฑ ๋ฌธ์ œ

     

    2470๋ฒˆ: ๋‘ ์šฉ์•ก

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

    www.acmicpc.net

     

     

    ๐ŸŒฑ ํ’€์ด

    ์ด ๋ฌธ์ œ๋Š” ํˆฌ ํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ํˆฌ ํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ž€, 1์ฐจ์› ๋ฐฐ์—ด์—์„œ ๊ฐ์ž ๋‹ค๋ฅธ ์›์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” 2๊ฐœ์˜ ํฌ์ธํ„ฐ๋ฅผ ์กฐ์ž‘ํ•ด๊ฐ€๋ฉด์„œ ์›ํ•˜๋Š” ๊ฐ’์„ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค.

     

    1. ์ž…๋ ฅ๋ฐ›๊ธฐ

    n = int(input())
    liquid = sorted(list(map(int, input().split())))

    ์ž…๋ ฅ์€ ๋‘ ์ค„์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ฒซ์งธ ์ค„์—๋Š” ์ „์ฒด ์šฉ์•ก์˜ ์ˆ˜์ธ n์„ ์ž…๋ ฅ๋ฐ›๊ณ , ๋‘˜์งธ ์ค„์—์„œ๋Š” ์šฉ์•ก์˜ ํŠน์„ฑ๊ฐ’๋“ค์ธ n๊ฐœ์˜ ์ •์ˆ˜๋“ค์„ ๋ฆฌ์ŠคํŠธ๋กœ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. ์ด๋•Œ, ๋ฆฌ์ŠคํŠธ๋Š” ์ •๋ ฌ์„ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

     

    2. ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”

    min = sys.maxsize
    start = 0
    end = n - 1
    result = []
    • min์€ ์šฉ์•ก์˜ ํŠน์„ฑ๊ฐ’์˜ ํ•ฉ์˜ ์ ˆ๋Œ“๊ฐ’ ์ค‘ ์ตœ์†Ÿ๊ฐ’์„ ๋‹ด๋Š” ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ํˆฌ ํฌ์ธํ„ฐ๋ฅผ ์กฐ์ž‘ํ•  ๋•Œ๋งˆ๋‹ค min๊ฐ’์ด ์—…๋ฐ์ดํŠธ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • start์™€ end๋Š” liquid ๋ฆฌ์ŠคํŠธ์˜ ๋‘ ์›์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํˆฌ ํฌ์ธํ„ฐ์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. liquid ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด๋ฏธ ์ •๋ ฌํ•ด์ฃผ์—ˆ์œผ๋ฏ€๋กœ ์–‘ ์ชฝ ๋์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ๋ฒ”์œ„๋ฅผ ์ค„์—ฌ๊ฐ‘๋‹ˆ๋‹ค.
    • result๋Š” ์ถœ๋ ฅํ•  ๊ฒฐ๊ณผ ๊ฐ’์„ ๋‹ด๋Š” ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค. min์ด ์—…๋ฐ์ดํŠธ๋  ๋•Œ๋งˆ๋‹ค result๋„ ์—…๋ฐ์ดํŠธ ๋ฉ๋‹ˆ๋‹ค.

     

    3. ํˆฌ ํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ฒ”์œ„๋ฅผ ์ค„์—ฌ๊ฐ€๊ธฐ

    while start < end:
        mix = liquid[start] + liquid[end]
    
        if abs(mix) < min:
            min = abs(mix)
            result = [liquid[start], liquid[end]]
    
        if mix < 0:
            start += 1
        elif mix > 0:
            end -= 1
        else:
            break
    • start์™€ end๊ฐ€ ๋งŒ๋‚˜๊ธฐ ์ „๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋ฉฐ, ๋‘ ์šฉ์•ก์˜ ํŠน์„ฑ๊ฐ’์˜ ํ•ฉ์ด 0์ด ๋˜๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ์ฆ‰์‹œ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.
    • mix๋Š” ๋‘ ์šฉ์•ก์˜ ํŠน์„ฑ๊ฐ’์„ ํ•ฉํ•œ ๊ฐ’์„ ๋‹ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์šฐ๋ฆฌ์˜ ๋ชฉํ‘œ๋Š” mix์˜ ์ ˆ๋Œ€๊ฐ’์ด 0๊ณผ ๊ฐ€์žฅ ๊ฐ€๊น๋„๋ก ํ•˜๋Š” ๋‘ ์šฉ์•ก์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ mix์˜ ์ ˆ๋Œ€๊ฐ’์„ ํ˜„์žฌ min๊ฐ’๊ณผ ๋น„๊ตํ•˜์—ฌ ๋” ์ž‘๋‹ค๋ฉด min๊ณผ result๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.
    • mix๊ฐ’์ด ์Œ์ˆ˜๋ผ๋ฉด ํˆฌ ํฌ์ธํ„ฐ ์กฐ์ž‘ ์‹œ, start๋ฅผ 1 ์ฆ๊ฐ€์‹œ์ผœ ์ดํ›„ ๊ณ„์‚ฐ๋  mix๊ฐ’์„ ์ฆ๊ฐ€์‹œ์ผœ 0์— ๊ฐ€๊น๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ min๊ฐ’์ด ์–‘์ˆ˜๋ผ๋ฉด ํˆฌ ํฌ์ธํ„ฐ ์กฐ์ž‘ ์‹œ, end๋ฅผ 1 ๊ฐ์†Œ์‹œ์ผœ mix๊ฐ’์ด 0์— ๊ฐ€๊น๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
    • mix๊ฐ’์ด 0์ด๋ผ๋ฉด ๋”์ด์ƒ ๊ฐœ์„ ํ•  ์‚ฌํ•ญ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.

     

    ๐ŸŒฑ ์ •๋‹ต ์ฝ”๋“œ

    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    # boj 2460 ๋‘ ์šฉ์•ก
    
    import sys
    
    input = sys.stdin.readline
    
    n = int(input())
    liquid = list(map(int, input().split()))
    
    min = sys.maxsize
    liquid.sort()
    start = 0
    end = n - 1
    result = []
    
    while start < end:
        mix = liquid[start] + liquid[end]
    
        if abs(mix) < min:
            min = abs(mix)
            result = [liquid[start], liquid[end]]
    
        if mix < 0:
            start += 1
        elif mix > 0:
            end -= 1
        else:
            break
    
    print(*result)
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€