๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/๋ฐฑ์ค€(BOJ)

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 1244 - ์Šค์œ„์น˜ ์ผœ๊ณ  ๋„๊ธฐ

by YWTechIT 2021. 5. 31.
728x90

๐Ÿ“ ๋ฐฑ์ค€ 1244 - ์Šค์œ„์น˜ ์ผœ๊ณ  ๋„๊ธฐ

๋ฐฑ์ค€ 1244 - ์Šค์œ„์น˜ ์ผœ๊ณ  ๋„๊ธฐ


โšก๏ธ ๋‚˜์˜ ํ’€์ด

๋ฌธ์ œ๊ฐ€ ์กฐ๊ธˆ ๊ธธ์—ˆ์ง€๋งŒ, ํ•ต์‹ฌ ํฌ์ธํŠธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

  1. ๋‚จํ•™์ƒ: ์ž๊ธฐ๊ฐ€ ๋ฐ›์€ ์ˆ˜์˜ ๋ฐฐ์ˆ˜์ธ ์Šค์œ„์น˜ ๋ฒˆํ˜ธ์˜ ์ƒํƒœ๋ฅผ ๋ฐ”๊พผ๋‹ค.
  2. ์—ฌํ•™์ƒ: ์ž๊ธฐ๊ฐ€ ๋ฐ›์€ ์ˆ˜์™€ ๊ฐ™์€ ๋ฒˆํ˜ธ์ธ ์Šค์œ„์น˜ ๋ฒˆํ˜ธ๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ์ขŒ์šฐ๊ฐ€ ๋Œ€์นญ์ด๋ฉด์„œ ๊ฐ€์žฅ ๋งŽ์€ ์Šค์œ„์น˜๋ฅผ ํฌํ•จํ•˜๋Š” ๊ตฌ๊ฐ„์˜ ์ƒํƒœ๋ฅผ ๋ชจ๋‘ ๋ณ€๊ฒฝํ•œ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์ฒ˜์Œ์— index๋ฅผ ํŽธํ•˜๊ฒŒ ๋ณด๊ธฐ ์œ„ํ•ด arr[0]์— ์ด ๋ฌธ์ œ์™€ ๊ด€๋ จ ์—†๋Š” ๊ฐ’์ธ -3333์„ ๋„ฃ์—ˆ๋‹ค.

 

๋‚จํ•™์ƒ์˜ ๊ฒฝ์šฐ๋Š” index๊ฐ€ ๋ฐฐ์ˆ˜์ธ ๊ฒฝ์šฐ๋ฅผ ์ฐพ์„ ๋•Œ if not i % target๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ๊ทธ๋Ÿด ํ•„์š” ์—†์ด ์‹œ์ž‘์ ์„ target์œผ๋กœ ํ•˜์—ฌ for i in range(target, n+1, target)์„ ์„ ์–ธํ•˜๋ฉด ๋ฐฐ์ˆ˜์˜ index๋งŒ ์ถœ๋ ฅ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์Šค์œ„์น˜์˜ ์ƒํƒœ๋ฅผ ๋ฐ”๊ฟ€ ๋•Œ 0 -> 1 1 -> 0 ๋ฐ–์— ์—†์œผ๋ฏ€๋กœ 1์„ ๋”ํ•œ ๊ฐ’์— 2๋กœ ๋‚˜๋ˆ„๋ฉด ์ฝ”๋“œ๋ฅผ ๋”์šฑ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์—ฌํ•™์ƒ์˜ ๊ฒฝ์šฐ๋Š” try, except ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, target์„ ์ค‘์‹ฌ์œผ๋กœ left_index, right_index๋ฅผ ์„ ์–ธํ•œ ๋‹ค์Œ left์™€ right๊ฐ€ ๊ฐ™์€์ง€ ๋น„๊ตํ•˜๊ณ  ๊ฐ™๋‹ค๋ฉด left์™€ right ๋ฒ”์œ„๋ฅผ ๋„“ํžˆ๊ณ  ๋‹ค๋ฅด๋‹ค๋ฉด ์ข…๋ฃŒ ์‹œํ‚ค๋Š” flag๋ฌธ์„ ์ ์šฉํ–ˆ๋‹ค.

 

๋˜, ๋ฒ”์œ„๋Š” ๋ฌดํ•œ์ •์œผ๋กœ ๋Š˜์–ด๋‚  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ arr[index]๊ฐ€ ๋ฒ—์–ด๋‚˜๋ฉด error๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”๋ฐ, ์ด๋•Œ try-except๋ฌธ์„ ์ž‘์„ฑํ•˜๋ฉด indexError ์— ๊ฑธ๋ฆฌ์ง€ ์•Š๋Š”๋‹ค. ์ดํ›„ ๋„“ํ˜€์ง„ ๋ฒ”์œ„๋งŒํผ ์Šค์œ„์น˜์˜ ์ƒํƒœ๋ฅผ ๋ฐ”๊ฟ”์•ผ ํ•˜๋Š”๋ฐ ์Šค์œ„์น˜๋Š” 0 -> 1 1 -> 0 ๋ฐ–์— ์—†์œผ๋ฏ€๋กœ 1์„ ๋”ํ•œ ๊ฐ’์— 2๋กœ ๋‚˜๋ˆ„๋ฉด ์ฝ”๋“œ๋ฅผ ๋”์šฑ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋งˆ์ง€๋ง‰์œผ๋กœ ์ถœ๋ ฅ์€ ํ•œ ์ค„์— 20๊ฐœ์”ฉ ๋Š์–ด์„œ ์ถœ๋ ฅํ•˜๋Š”๋ฐ ์ด๋•Œ index๋ฅผ 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ i๊ฐ€ 20๊ณผ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง„๋‹ค๋ฉด ์ค„๋ฐ”๊ฟˆํ•ด์ฃผ๋Š” ์ฝ”๋“œ์ธ print()๋ฅผ ์ฃผ๋ฉด๋œ๋‹ค.

 

# ๋‚˜์˜ ์ฝ”๋“œ
n = int(input())
arr = [-3333] + list(map(int, input().split()))    # append useless index arr[0] 

def male(switch, target):    # male case
    for i in range(1, n + 1):    # change switch state
        if not i % target:
            if not switch[i]:
                switch[i] = 1
            else:
                switch[i] = 0
    return switch

def female(switch, target):    # female case
    left, right = target - 1, target + 1
    flag = True

    try:    # prevent indexError
        while flag:
            if switch[left] == switch[right]:
                left -= 1
                right += 1
            else:
                flag = False
    except:
        pass

    for i in range(left + 1, right):    # change switch state
        if not switch[i]:
            switch[i] = 1
        else:
            switch[i] = 0
    return switch

student = int(input())
for _ in range(student):
    sex, number = map(int, input().split())
    if sex == 1:
        male(arr, number)
    else:
        female(arr, number)
    print(arr)

for i in range(1, len(arr)):    # print each 20 index
    print(arr[i], end=' ')
    if not i % 20:
        print()

 

 

# ๊ฐ„๊ฒฐํ•œ ์ฝ”๋“œ
n = int(input())
arr = [-3333] + list(map(int, input().split()))    # append useless index arr[0] 

def male(switch, target):    # male case
    for i in range(target, n + 1, target):    # change switch state
        switch[i] = (switch[i] + 1) % 2
    return switch

def female(switch, target):    # female case
    left, right = target - 1, target + 1
    flag = True

    try:    # prevent indexError
        while flag:
            if switch[left] == switch[right]:
                left -= 1
                right += 1
            else:
                flag = False
    except:
        pass

    for i in range(left + 1, right):    # change switch state
        switch[i] = (switch[i] + 1) % 2
    return switch

student = int(input())
for _ in range(student):
    sex, number = map(int, input().split())
    if sex == 1:
        male(arr, number)
    else:
        female(arr, number)

for i in range(1, len(arr)):    # print each 20 index
    print(arr[i], end=' ')
    if not i % 20:
        print()
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€