์•Œ๊ณ ๋ฆฌ์ฆ˜/์ด๋ถ„ํƒ์ƒ‰

[๋ฐฑ์ค€ 19637 ๐Ÿฅˆ] IF๋ฌธ ์ข€ ๋Œ€์‹  ์จ์ค˜

1eehyunji 2023. 10. 7. 19:34

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

 

19637๋ฒˆ: IF๋ฌธ ์ข€ ๋Œ€์‹  ์จ์ค˜

์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ์นญํ˜ธ์˜ ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 105)๊ณผ ์นญํ˜ธ๋ฅผ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋Š” ์บ๋ฆญํ„ฐ๋“ค์˜ ๊ฐœ์ˆ˜ M (1 ≤ M ≤ 105)์ด ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N, M ≤ 105) ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฐ ์นญ

www.acmicpc.net

๋ฌธ์ œ

๊ฒŒ์ž„ ๊ฐœ๋ฐœ์ž์ธ ๋ฐ€๋ฆฌ๋Š” ์ „ํˆฌ๋ ฅ ์‹œ์Šคํ…œ์„ ๋งŒ๋“ค์–ด, ์บ๋ฆญํ„ฐ๊ฐ€ ๊ฐ€์ง„ ์ „ํˆฌ๋ ฅ์„ ๊ธฐ์ค€์œผ๋กœ ์นญํ˜ธ๋ฅผ ๋ถ™์—ฌ์ฃผ๋ ค๊ณ  ํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์ „ํˆฌ๋ ฅ 10,000 ์ดํ•˜์˜ ์บ๋ฆญํ„ฐ๋Š” WEAK, 10,000 ์ดˆ๊ณผ ๊ทธ๋ฆฌ๊ณ  100,000 ์ดํ•˜์˜ ์บ๋ฆญํ„ฐ๋Š” NORMAL, 100,000 ์ดˆ๊ณผ ๊ทธ๋ฆฌ๊ณ  1,000,000 ์ดํ•˜์˜ ์บ๋ฆญํ„ฐ๋Š” STRONG ์นญํ˜ธ๋ฅผ ๋ถ™์—ฌ์ค€๋‹ค๊ณ  ํ•˜์ž. ์ด๋ฅผ IF๋ฌธ์œผ๋กœ ์ž‘์„ฑํ•œ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

if power <= 10000
 print WEAK
else if power <= 100000
 print NORMAL
else if power <= 1000000
 print STRONG

ํ˜ผ์ž์„œ ๊ฒŒ์ž„์„ ๊ฐœ๋ฐœํ•˜๋А๋ผ ๋งค์šฐ ๋ฐ”์œ ๋ฐ€๋ฆฌ๋ฅผ ๋Œ€์‹ ํ•ด์„œ, ์บ๋ฆญํ„ฐ์˜ ์ „ํˆฌ๋ ฅ์— ๋งž๋Š” ์นญํ˜ธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์ž.

์ž…๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ์นญํ˜ธ์˜ ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 10^5)๊ณผ ์นญํ˜ธ๋ฅผ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋Š” ์บ๋ฆญํ„ฐ๋“ค์˜ ๊ฐœ์ˆ˜ M (1 ≤ M ≤ 10^5)์ด ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N, M ≤ 10^5)

๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฐ ์นญํ˜ธ์˜ ์ด๋ฆ„์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ธธ์ด 1 ์ด์ƒ, 11 ์ดํ•˜์˜ ์˜์–ด ๋Œ€๋ฌธ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด๊ณผ ํ•ด๋‹น ์นญํ˜ธ์˜ ์ „ํˆฌ๋ ฅ ์ƒํ•œ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š” 10^9 ์ดํ•˜์˜ ์Œ์ด ์•„๋‹Œ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์นญํ˜ธ๋Š” ์ „ํˆฌ๋ ฅ ์ƒํ•œ๊ฐ’์˜ ๋น„๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค. 

N + 2๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ๊ฐ ์ค„์—๋Š” ์บ๋ฆญํ„ฐ์˜ ์ „ํˆฌ๋ ฅ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์Œ์ด ์•„๋‹Œ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ํ•ด๋‹นํ•˜๋Š” ์นญํ˜ธ๊ฐ€ ์—†๋Š” ์ „ํˆฌ๋ ฅ์€ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€์ง€ ์•Š๋Š”๋‹ค.

์ถœ๋ ฅ

M๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ์บ๋ฆญํ„ฐ์˜ ์ „ํˆฌ๋ ฅ์— ๋งž๋Š” ์นญํ˜ธ๋ฅผ ์ž…๋ ฅ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•œ๋‹ค. ์–ด๋–ค ์บ๋ฆญํ„ฐ์˜ ์ „ํˆฌ๋ ฅ์œผ๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ์นญํ˜ธ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ ๊ฐ€์žฅ ๋จผ์ € ์ž…๋ ฅ๋œ ์นญํ˜ธ ํ•˜๋‚˜๋งŒ ์ถœ๋ ฅํ•œ๋‹ค.

๋ฌธ์ œ ํ’€์ด

import sys
from bisect import bisect_left

n,m=map(int, sys.stdin.readline().split(' '))
level_info=[]
rate_info=[]
for _ in range(n):
    l, r = sys.stdin.readline().strip().split(' ')
    level_info.append(l)
    rate_info.append(int(r))

for _ in range(m):
    rate=int(sys.stdin.readline())
    # ํ•ด๋‹น rate๊ฐ€ ์–ด๋А ๋ฒ”์œ„์— ํ•ด๋‹นํ•˜๋Š”์ง€ ์ด๋ถ„ํƒ์ƒ‰์œผ๋กœ ํƒ์ƒ‰ํ•œ๋‹ค.
    # ๋‹จ์ˆœ for๋ฌธ์„ ์ด์šฉํ•ด์„œ ํƒ์ƒ‰ํ•  ๊ฒฝ์šฐ ์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ์บ๋ฆญํ„ฐ์˜ ๋ ˆ๋ฒจ์„ ํŒŒ์•…ํ•˜๋Š” ๋ฐ์— 10^5 * 10^5 ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚  ์ˆ˜ ์žˆ๋‹ค.
    # ์ง์ ‘ ๊ตฌํ˜„
    start, end =0, n-1
    result=''
    while start<=end:
        mid=(start+end)//2
        # mid๋ฒˆ์งธ ๋ ˆ๋ฒจ์ด rate๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค๋ฉด, ๋” ์ž‘์€ ๋ ˆ๋ฒจ์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ํ™•์ธ
        if rate_info[mid]>=rate:
            end=mid-1
            result=level_info[mid]
        else:
            start=mid+1
    print(result)

    # bisect ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ
    # rl=bisect_left(rate_info, rate)
    # print(level_info[rl])