Hello Ocean! ๐ŸŒผ

[๋ฐฑ์ค€/Python] 12757. ์ „์„ค์˜ JBNU ๋ณธ๋ฌธ

Algorithm

[๋ฐฑ์ค€/Python] 12757. ์ „์„ค์˜ JBNU

bba_dda 2021. 10. 6. 18:30
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

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

 

12757๋ฒˆ: ์ „์„ค์˜ JBNU

์ฒซ ์ค„์—๋Š” ์ดˆ๊ธฐ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜์ธ \(N(1 \le N \le 100,000)\) ๊ณผ ๋ช…๋ น ํšŸ์ˆ˜์ธ \(M(1 \le M \le 100,000)\), ๊ฐ€์žฅ ๊ทผ์ ‘ํ•œ Key๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ์˜ ์ œํ•œ์ธ \(K(1 \le K \le 10,000)\)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.  ์ž…๋ ฅ์˜ ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—

www.acmicpc.net

๋ฌธ์ œ์—์„œ 

M๊ฐœ์˜ ๋ช…๋ น๋ฌธ์„ ์ฒ˜๋ฆฌํ•  ๋•Œ "2 num val" ์— ๋Œ€ํ•ด์„œ 

num์œผ๋กœ ๊ฒ€์ƒ‰๋œ key์˜ val์„ ๋ฐ”๊พธ๋ผ๊ณ  ํ–ˆ๋Š”๋ฐ, 

2๊ฐœ์˜ key๊ฐ€ ๋ชจ๋‘ ์กด์žฌํ•˜๋Š” ์ƒํ™ฉ์—์„œ ๋‘˜๋‹ค ๋ฐ”๊พธ๋Š”๊ฑด์ง€, ์•„๋‹ˆ๋ฉด ์•„์˜ˆ ์•ˆ๋ฐ”๊พธ๋Š”๊ฑด์ง€ ๋ช…์‹œํ•ด์ฃผ์ง€ ์•Š์•„์„œ ํ—ท๊ฐˆ๋ ธ๋‹ค. 

 

๋‘˜๋‹ค ํ•ด๋ณด๋‹ˆ๊นŒ ์•„์˜ˆ ์•ˆ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด ์ •๋‹ต์ด์—ˆ๋‹ค. ๋ฌธ์ œ์—์„œ ์ด ๋ถ€๋ถ„์„ ๋ช…ํ™•ํ•˜๊ฒŒ ํ‘œํ˜„ํ•ด์ฃผ๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์•˜๋‹ค. 

ํ’€์ด

์–ด๋–ค ์ˆซ์ž๊ฐ€ ๋“ค์–ด์™”์„ ๋•Œ, JBNU์•ˆ์—์„œ ๊ทธ ์ˆซ์ž์™€ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šฐ๋ฉด์„œ K์ดํ•˜๋กœ ์ฐจ์ด๋‚˜๋Š” key๋ฅผ ์ฐพ์•„์•ผ ํ–ˆ๋‹ค. 

์‹ค์ œ ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋Š” key๋“ค์˜ list์ธ keys๋ฅผ ์ด๋ถ„ํƒ์ƒ‰์„ ํ†ตํ•ด ์‚ฝ์ž…ํ•ด์„œ ํ•ญ์ƒ ์ •๋ ฌ๋œ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๋„๋ก ํ–ˆ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  ์–ด๋–ค ์ˆซ์ž์— ๋Œ€ํ•ด ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด key๋ฅผ ์ฐพ์„ ๋•Œ์—๋„ ์ด๋ถ„ํƒ์ƒ‰์„ ์ด์šฉํ–ˆ๋‹ค. 

 

[1, 2, 5] ์ƒํƒœ์—์„œ 4๋ฅผ ์ด๋ถ„ํƒ์ƒ‰ํ•˜๋ฉด 2๊ฐ€ ๋‚˜์˜จ๋‹ค. ๋”ฐ๋ผ์„œ 2-1 = 1 ๋ฒˆ์งธ ๊ฐ’๊ณผ 2๋ฒˆ์งธ ๊ฐ’์„ ๋น„๊ตํ•ด์„œ ๋‘ ๊ฐ’์ค‘์— ์–ด๋–ค ๊ฒƒ์ด 4์™€ ๋” ๊ฐ€๊นŒ์šด์ง€ ๋น„๊ตํ•˜์˜€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ๊ฐ’๊ณผ 4์˜ ์ฐจ์ด๊ฐ€ ๋™์ผํ•˜๋ฉด -2๋ฅผ returnํ•˜์—ฌ ์ฐพ์€ ๊ฐ’์ด 2๊ฐœ์ž„์„ ๋‚˜ํƒ€๋‚ด์ฃผ์—ˆ๋‹ค. 

 

์ด๋ถ„ํƒ์ƒ‰์€ ์ง์ ‘ ๊ตฌํ˜„ํ•˜์ง€ ์•Š๊ณ  python bisect ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์˜€๋‹ค. 

๊ณต์‹ ๋ฌธ์„œ : https://docs.python.org/ko/3/library/bisect.html

 

๊ธฐ๋ณธ input์„ ์ผ๋”๋‹ˆ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ ๋‘์ค„์„ ๋„ฃ์–ด input๋Œ€์‹ ์— sys.stdin.readline์„ ์‚ฌ์šฉํ–ˆ๋‹ค. 

import sys
input = sys.stdin.readline

์ฝ”๋“œ

import bisect
import sys

input = sys.stdin.readline
inputs = input().split()
N = int(inputs[0]) 
M = int(inputs[1])
K = int(inputs[2]) 

jbnu = dict() # key - value ์ €์žฅํ•  dict 
keys = [] # key ์ €์žฅ list, ์ •๋ ฌ๋œ ์ƒํƒœ, ์ด๋ถ„ํƒ์ƒ‰์œผ๋กœ key ์‚ฝ์ž… 

def addKey(key):
    idx = bisect.bisect_left(keys, key)
    keys.insert(idx,key)
def findKey(num):
    val = jbnu.get(num, -1)
    if val != -1:
        return num
    idx = bisect.bisect(keys, num) # 
    if idx == 0: # ๋งจ ์•ž
        if abs(keys[idx] - num) <= K:
            return keys[idx]
    elif idx == len(keys): #๋งจ ๋’ค
        if abs(num - keys[idx-1]) <= K:
            return keys[idx-1]
    else:
        left = num - keys[idx-1]
        right = keys[idx] - num
        if left == right and left <= K:
            return -2 # ๋‘ ๊ฐœ ๊ฒ€์ƒ‰๋จ 
        if left < right and left <= K:
            return keys[idx-1]
        if right < left and right <= K:
            return keys[idx]
    return val # -1 
    
        
for n in range(N):
    inputs = input().split()
    key = int(inputs[0])
    val = int(inputs[1])
    # key - k ~ key + k๊นŒ์ง€ ์ „๋ถ€ ์ฑ„์šฐ๊ธฐ 
    jbnu[key] = val
    addKey(key)
for m in range(M):
    inputs = input().split()
    if inputs[0] == '1': # INSERT
        key = int(inputs[1])
        val = int(inputs[2])
        jbnu[key] = val
        addKey(key)
    elif inputs[0] == '2': # UPDATE
        num = int(inputs[1])
        val = int(inputs[2])
        key = findKey(num) # ๋‘๊ฐœ๊ฐ€ ๊ฒ€์ƒ‰๋˜๋ฉด ๊ฑ ์•”๊ฒƒ๋„ ์•ˆ๋ฐ”๊ฟˆ 
        if key >= 0:
            jbnu[key] = val
    else:
        num = int(inputs[1])
        key = findKey(num)
        if key == -2:
            print('?')
        elif key == -1:
            print(-1)
        else:
            print(jbnu[key])

๊ฒฐ๊ณผ

๋ฐ˜์‘ํ˜•