๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’๊ณต๋ถ€/์•Œ๊ณ ๋ฆฌ์ฆ˜

[๋ฐฑ์ค€] 2805๋ฒˆ ๋‚˜๋ฌด ์ž๋ฅด๊ธฐ

by rindev 2020. 11. 24.

๋‚˜๋ฌด๋ฅผ ์™œ์ž๋ฅด๋‹ˆ ์–˜๋“ค์•„..

 

www.acmicpc.net/problem/2805

 

2805๋ฒˆ: ๋‚˜๋ฌด ์ž๋ฅด๊ธฐ

์ฒซ์งธ ์ค„์— ๋‚˜๋ฌด์˜ ์ˆ˜ N๊ณผ ์ƒ๊ทผ์ด๊ฐ€ ์ง‘์œผ๋กœ ๊ฐ€์ ธ๊ฐ€๋ ค๊ณ  ํ•˜๋Š” ๋‚˜๋ฌด์˜ ๊ธธ์ด M์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 1,000,000, 1 ≤ M ≤ 2,000,000,000) ๋‘˜์งธ ์ค„์—๋Š” ๋‚˜๋ฌด์˜ ๋†’์ด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‚˜๋ฌด์˜ ๋†’์ด์˜ ํ•ฉ์€ ํ•ญ์ƒ M์„

www.acmicpc.net

 

์•”ํŠผ ๋‚˜๋ฌด๋ฅผ ์ž๋ฅด๋Š”๊ฑธ... ๋•๊ธฐ๋กœํ•ฉ๋‹ˆ๋‹ค

 

๐Ÿƒ‍โ™€๏ธ์ž‘์„ฑํ•œ ์ฝ”๋“œ

๋”๋ณด๊ธฐ
# 2805 ๋ฒˆ : ๋‚˜๋ฌด ์ž๋ฅด๊ธฐ
def binary_search_2805():
  n, m = map(int, input().split())
  trees = list(map(int, input().split()))

  # n = 4
  # m = 7
  # trees = [20, 15, 10, 17]

  s = 0
  e = max(trees)
  answer = 0

  while s <= e :
    c = (s+e) // 2
    # print('s,e,c====', s,e,c)

    hap = 0
    for t in trees:
      if t >= c:
        tmp = t - c
        hap += tmp
        # print('tmp, hap', tmp, hap)
    
    if hap < m : #๊ธธ์ด๊ฐ€ ๋ชจ์ž๋žŒ
      e = c - 1
    elif hap >= m : #๊ธธ์ด๊ฐ€ ๋„˜์นจ
      s = c + 1
      answer = c 
  print(answer)

 

์ผ๋‹จ s,c,e๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค(start, center, end์˜ ์•ฝ์ž...)

๊ทธ๋ž˜์„œ ๊ฐ€์ ธ๊ฐ€๋Š” ๋‚˜๋ฌด์˜ ํ•ฉ(hap)์ด ๋‚ด๊ฐ€ ํ•„์š”ํ•œ ๊ธธ์ด m๋ณด๋‹ค ์ž‘์œผ๋ฉด(๋ชจ์ž๋ผ๋ฉด)

e = c -1 ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ H๋ฅผ ๋‚ด๋ฆฐ๋‹ค.  -> ๋‚˜๋ฌด๋ฅผ ๋” ์ž๋ฅด๊ฒŒ ๋จ

 

hap์ด m๋ณด๋‹ค ํฌ๋‹ค๋ฉด (ํ•„์š”ํ•œ ๊ธธ์ด๋ณด๋‹ค ๋งŽ์ด ์ž๋ฆ„) 

s = c+1 ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ H๋ฅผ ์˜ฌ๋ฆฐ๋‹ค. ->๋‚˜๋ฌด๋ฅผ ๋œ ์ž๋ฅด๊ฒŒ ๋จ

 

์ด๋ฅผ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ์ ์ ˆํ•œ ๊ธธ์ด๋ฅผ ์ฐพ๋Š”๋‹ค.

๋Œ“๊ธ€