์ฐธ๊ณ ์ž๋ฃŒ : ์ด๊ฒƒ์ด ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋‹ค (๋‚˜๋™๋นˆ ์ €)

1. ๋ฒ”์œ„๋ฅผ ๋ฐ˜์”ฉ ์ขํ˜€๊ฐ€๋Š” ํƒ์ƒ‰

์ˆœ์ฐจํƒ์ƒ‰

  • ์ˆœ์ฐจํƒ์ƒ‰ : ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์žˆ๋Š” ํŠน์ •ํ•˜ ใ„ด๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ์•ž์—์„œ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์”ฉ ์ฐจ๋ก€๋Œ€๋กœ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•
def seq_search(n, target, array):
    for i in range(n):
        if array[i] == target:
            return i+1

input_data = input().split()
n = int(input_data[0])
target = input_data[1]
array = input().split()

print(seq_search(n, target, array))
  • ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ N๊ฐœ์ผ ๋•Œ ์ตœ๋Œ€ N๋ฒˆ์˜ ๋น„๊ต ์—ฐ์‚ฐ์ด ํ•„์š”ํ•˜๋ฏ€๋กœ ์ˆœ์ฐจ ํƒ์ƒ‰์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(N)์ด๋‹ค.

์ด์ง„ํƒ์ƒ‰

  • ์ด์ง„ ํƒ์ƒ‰์€ ๋ฐฐ์—ด ๋‚ด๋ถ€์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ •๋ ฌ๋˜์–ด ์žˆ์„ ๋•Œ ๋ฒ”์œ„๋ฅผ ์ ˆ๋ฐ˜์”ฉ ์ขํ˜€๊ฐ€๋ฉฐ ๋ฐ์ดํ„ฐ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • ์ด๋ฏธ ์ •๋ ฌ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ ๋งค์šฐ ๋น ๋ฅด๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.
  • ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜ 3๊ฐœ ์‹œ์ž‘์  ๋์  ์ค‘๊ฐ„์  ์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • ์‹œ๊ฐ„๋ณต์žก๋„ O(logN)

์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ

def bin_search(array, target, start, end):
    if start > end: return None
    mid = (start + end) // 2

    if array[mid] == target: return mid
    elif array[mid] > target : 
        return bin_search(array, target, start, mid-1)
    else :
        return bin_search(array, target, mid+1, end)

๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ

def bin_search(array, target, start, end):
    while start <= end:
        mid = (start + end) // 2

        if array[mid] == target: return mid
        elif array[mid] > target : 
            end = mid - 1
        else :
            start = mid + 1

    return None

์ฝ”ํ…Œ์—์„œ์˜ ์ด์ง„ํƒ์ƒ‰

  • ์—ฌ๋Ÿฌ ์ฐจ๋ก€ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜๋ฉฐ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์™ธ์šฐ์ž! ์ค‘์š”์ค‘์š”~
  • ํƒ์ƒ‰ ๋ฒ”์œ„๊ฐ€ ํด ๋•Œ๋Š” ์ด์ง„ํƒ์ƒ‰์œผ๋กœ ์ ‘๊ทผํ•˜์ž.

ํŠธ๋ฆฌ ์ž๋ฃŒ๊ตฌ์กฐ

  • ํŠธ๋ฆฌ ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ๋…ธ๋“œ์™€ ๋…ธ๋“œ์˜ ์—ฐ๊ฒฐ๋กœ ํ‘œํ˜„ํ•˜๋ฉฐ ๋งŽ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.
  • ํŠธ๋ฆฌ๋Š” ์ผ๋ถ€๋ฅผ ๋–ผ์–ด๋‚ด๋„ ํŠธ๋ฆฌ ๊ตฌ์กฐ์ด๋ฉฐ ์ด๋ฅผ ์„œ๋ธŒ ํŠธ๋ฆฌ๋ผ ํ•œ๋‹ค.

์ด์ง„ ํƒ์ƒ‰ํŠธ๋ฆฌ

  • ํŠธ๋ฆฌ ์ž๋ฃŒ๊ตฌ์กฐ ์ค‘์—์„œ ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ํ˜•ํƒœ๊ฐ€ ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ์ด๋‹ค.
  • ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ : ์ด์ง„ ํƒ์ƒ‰์ด ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ณ ์•ˆ๋œ, ํšจ์œจ์  ํƒ์ƒ‰์ด ๊ฐ€๋Šฅํ•œ ์ž๋ฃŒ๊ตฌ์กฐ
  1. ๋ถ€๋ชจ ๋…ธ๋“œ๋ณด๋‹ค ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์ž‘๋‹ค.
  2. ๋ถ€๋ชจ ๋…ธ๋“œ๋ณด๋‹ค ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ํฌ๋‹ค

๋น ๋ฅด๊ฒŒ ์ž…๋ ฅ๋ฐ›๊ธฐ

  • Python input() ํ•จ์ˆ˜๋Š” ๋™์ž‘ ์†๋„๊ฐ€ ๋Š๋ฆฌ๋‹ค.
  • ํ•œ ์ค„ ์ž…๋ ฅ๋ฐ›๊ณ  rstrip() ํ•จ์ˆ˜๋ฅผ ๊ผญ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค.
import sys

input_data = sys.stdin.readline().rstrip()
print(input_data)

2. ๋ถ€ํ’ˆ์ฐพ๊ธฐ

๋‚œ์ด๋„ ์ค‘ํ•˜

๋ฌธ์ œ

์ „์ž ๋งค์žฅ์—๋Š” ๋ถ€ํ’ˆ์ด N๊ฐœ ์žˆ๊ณ  ๊ฐ ๋ถ€ํ’ˆ์—๋Š” ์ •์ˆ˜ ํ˜•ํƒœ์˜ ๊ณ ์œ ๋ฒˆํ˜ธ๊ฐ€ ์žˆ๋‹ค. ์–ด๋Š” ๋‚  ์†๋‹˜์ด M๊ฐœ ์ข…๋ฅ˜์˜ ๋ถ€ํ’ˆ์„ ๋Œ€๋Ÿ‰์œผ๋กœ ๊ตฌ๋งคํ•˜๊ฒ ๋‹ค๋ฉฐ ๋‹น์ผ ๋‚  ๊ฒฌ์ ์„œ๋ฅผ ์š”์ฒญํ–ˆ๋‹ค. ์†๋‹˜์ด ๋ฌธ์˜ํ•œ ๋ถ€ํ’ˆ M๊ฐœ ์ข…๋ฅ˜๋ฅผ ๋ชจ๋‘ ํ™•์ธํ•ด์„œ ๊ฒฌ์ ์„œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. ์ด๋•Œ ๊ฐ€๊ฒŒ ์•ˆ์— ๋ถ€ํ’ˆ์ด ๋ชจ๋‘ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด๋ณด์ž.

ํ•ด์„ค

๋จผ์ € ๋งค์žฅ ๋‚ด N๊ฐœ์˜ ๋ถ€ํ’ˆ์„ ๋ฒˆํ˜ธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  M๊ฐœ์˜ ๋ถ€ํ’ˆ์„ ๊ฐ๊ฐ ์ด์ง„ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

์ฝ”๋“œ

def bin_search(array, target, start, end):
    while start <= end:
        mid = (start + end) // 2
        if array[mid] == target:
            return mid
        elif array[mid] > target:
            end = mid - 1
        else:
            start = mid + 1
    return None

    n = int(input())
    array = list(map(int, input().split()))
    array.sort()
    m = int(input())
    x = list(map(int, input().split()))

    for i in x :
        result = bin_search(array, i, 0, n-1)
        if result != None : print('yes', end=' ')
        else : print('no', end=' ')

๊ณ„์ˆ˜ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค

n = int(input())
array = [0] * 1000001

for i in input().split():
    array[int(i)] += 1

m = int(input())
x = list(map(int, input().split()))

for i in x :
    if array[i] == 1 : print('yes', end=' ')
    else : print('no', end=' ')

๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•  ๋•Œ๋Š” ์ง‘ํ•ฉ ์ž๋ฃŒํ˜•์ด ์ข‹๋‹ค.

n = int(input())
array = set(map(int, input().split()))
m = int(input())
x = list(map(int, input().split()))

for i in x :
    if i in array : print('yes', end=' ')
    else : print('no', end=' ')

3. ๋–ก๋ณถ์ด ๋–ก ๋งŒ๋“ค๊ธฐ

๋‚œ์ด๋„ ์ค‘

๋ฌธ์ œ

๋™๋นˆ์ด๋„ค ๋–ก๋ณถ์ด ๋–ก์€ ๋–ก์˜ ๊ธธ์ด๊ฐ€ ์ผ์ •ํ•˜์ง€ ์•Š๊ณ  ๋Œ€์‹ ์— ํ•œ ๋ด‰์ง€ ์•ˆ์— ๋“ค์–ด๊ฐ€๋Š” ๋–ก์˜ ์ด ๊ธธ์ด๋Š” ์ ˆ๋‹จ๊ธฐ๋กœ ์ž˜๋ผ์„œ ๋งž์ถฐ์ค€๋‹ค. ์ ˆ๋‹ค๊ธฐ์— ๋†’์ด(H)๋ฅผ ์ง€์ •ํ•˜๋ฉด ์ค„์ง€์–ด์ง„ ๋–ก์„ ํ•œ ๋ฒˆ์— ์ ˆ๋‹จํ•œ๋‹ค. ๋†’์ด H๋ณด๋‹ค ๊ธด ๋–ก์€ ์ž˜๋ฆฌ๊ณ , ๋‚ฎ์€ ๋–ก์€ ์ž˜๋ฆฌ์ง€ ์•Š๋Š”๋‹ค. ์†๋‹˜์ด ์™”์„ ๋•Œ ์š”์ฒญํ•œ ์ด ๊ธธ์ด๊ฐ€ M์ผ ๋•Œ ์ ์–ด๋„ M๋งŒํผ์˜ ๋–ก์„ ์–ป๊ธฐ ์œ„ํ•ด ์ ˆ๋‹จ๊ธฐ์— ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋†’์ด์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

ํ•ด์„ค

์ „ํ˜•์ ์ธ ์ด์ง„ ํƒ์ƒ‰ ๋ฌธ์ œ์ด์ž, ํŒŒ๋ผ๋ฉ”ํŠธ๋ฆญ ์„œ์น˜์œ ํ˜•์ด๋‹ค. ํŒŒ๋ผ๋ฉ”ํŠธ๋ฆญ ์„œ์น˜๋Š” ์ตœ์ ํ™” ๋ฌธ์ œ๋ฅผ ๊ฒฐ์ • ๋ฌธ์ œ๋กœ ๋ฐ”๊พธ์–ด ํ•ด๊ฒฐํ•˜๋Š” ๊ธฐ๋ฒ•์ด๋‹ค. ์ด ๋ฌธ์ œ์˜ ํ’€์ด ์•„์ด๋””์–ด๋Š” ์˜์™ธ๋กœ ๊ฐ„๋‹จํ•œ๋ฐ ์ ์ ˆํ•œ ๋†’์ด๋ฅผ ์ฐพ์„ ๋•Œ๊นŒ์ง€ ์ ˆ๋‹จ๊ธฐ์˜ ๋†’์ด H๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ์กฐ์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์ฝ”๋“œ

n, m = map(int, input().split())
array = list(map(int, input().split()))
start = 0
end = max(array)

result = 0
while(start<=end):
    total = 0
    mid = (start+end) // 2
    for x in array:
        if x > mid:
            total += x - mid

    if total < m:
        end = mid - 1
    else:
        result = mid
        start = mid + 1

print(result)