ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python] ๋ฐฑ์ค€ 19637 IF๋ฌธ ์ข€ ๋Œ€์‹  ์จ์ค˜ (Binary Search)
    ๐Ÿ’ป Algorithm/Python 2022. 7. 8. 19:33

    ๐Ÿ“Œ ๋ฌธ์ œ

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

     

    ์˜ˆ๋ฅผ ๋“ค์–ด, ์ „ํˆฌ๋ ฅ 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 ≤ 105)๊ณผ ์นญํ˜ธ๋ฅผ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋Š” ์บ๋ฆญํ„ฐ๋“ค์˜ ๊ฐœ์ˆ˜ M (1 ≤ M ≤ 105)์ด ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N, M ≤ 105)

     

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

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

    ์ถœ๋ ฅ

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

    ์˜ˆ์ œ ์ž…๋ ฅ 1

    3 8
    WEAK 10000
    NORMAL 100000
    STRONG 1000000
    0
    9999
    10000
    10001
    50000
    100000
    500000
    1000000

    ์˜ˆ์ œ ์ถœ๋ ฅ 1

    WEAK
    WEAK
    WEAK
    NORMAL
    NORMAL
    NORMAL
    STRONG
    STRONG

    ์˜ˆ์ œ ์ž…๋ ฅ 2

    3 5
    B 100
    A 100
    C 1000
    99
    100
    101
    500
    1000

    ์˜ˆ์ œ ์ถœ๋ ฅ 2

    B
    B
    C
    C
    C

     


    ๐Ÿ“Œ ํ’€์ด

    import sys
    from bisect import bisect_left
    input = sys.stdin.readline
    
    n, m = map(int, input().split())
    title = []
    power = []
    
    for _ in range(n):
        a, b = input().split()
        title.append(a)
        power.append(int(b))
    
    for _ in range(m):
        print(title[bisect_left(power, int(input()))])

     

    ์˜ˆ์ œ 2์˜ ๊ฒฝ์šฐ,

    title = [B, A, C]
    power = [100, 100, 1000]
    # input ์ฐจ๋ก€๋กœ 99, 100, 101, 500, 1000

    bisect_left(power, 99)๋Š” 0์„ ๋ฐ˜ํ™˜ → title[0] = B
    bisect_left(power, 100)์€ 0์„ ๋ฐ˜ํ™˜ → title[0] = B
    bisect_left(power, 101)์€ 2๋ฅผ ๋ฐ˜ํ™˜ → title[2] = C

    bisect ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

    bisect(a, b)๋Š” b๊ฐ€ a์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

    a = [1, 5, 6, 7, 10]
    b = 6
    print(bisect(a, b)) # 3

    a = [1, 5, 6, 7, 10], b = 6์ด๋ฉด b๊ฐ€ 5์™€ 6 ์‚ฌ์ด ๋˜๋Š” 6๊ณผ 7 ์‚ฌ์ด์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์œผ๋‚˜ bisect(a, b)๋Š” ๊ฐ€๋Šฅํ•œ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ์˜ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 6๊ณผ 7 ์‚ฌ์ด ์œ„์น˜์ธ ์ธ๋ฑ์Šค์ธ 3์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

    a = [1, 5, 6, 7, 10]
    b = 6
    print(bisect_left(a, b)) # 2

    ๊ฐ€๋Šฅํ•œ ๊ฐ€์žฅ ์™ผ์ชฝ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ ค๋ฉด bisect์˜ bisect_left๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. bisect_right๋„ ์žˆ๋Š”๋ฐ, ์ด๋Š” bisect_left์™€ ํ†ค์•ค๋งค๋„ˆ๋ฅผ ๋งž์ถ”๊ธฐ ์œ„ํ•œ ๊ฒƒ์ผ ๋ฟ bisect์™€ ์—ญํ• ์€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

     

     

     

    ๋Œ“๊ธ€

Designed by Tistory.