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

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 14467 - ์†Œ๊ฐ€ ๊ธธ์„ ๊ฑด๋„ˆ๊ฐ„ ์ด์œ 1

by YWTechIT 2021. 4. 28.
728x90

๐Ÿ“ ๋ฐฑ์ค€ 14467 - ์†Œ๊ฐ€ ๊ธธ์„ ๊ฑด๋„ˆ๊ฐ„ ์ด์œ  1

๋ฐฑ์ค€ 14467 - ์†Œ๊ฐ€ ๊ธธ์„ ๊ฑด๋„ˆ๊ฐ„ ์ด์œ 1


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

์ด๋ฒˆ์—๋„ defaultdict๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์—ˆ๋‹ค.

 

  1. 1๋ฒˆ๋ถ€ํ„ฐ 10๋ฒˆ๊นŒ์ง€ ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ์„ ์–ธํ•œ๋‹ค.
  2. ์†Œ์˜ ์œ„์น˜๋ฅผ ๋นˆ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•œ๋‹ค.
  3. cnt๋Š” 2๋ฒˆ ์ด์ƒ ์›€์ง์ธ ์†Œ๋ถ€ํ„ฐ ์ฆ๊ฐ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๊ฐ’๋งŒ ๋”ฐ๋กœ ๋นผ๋ƒˆ๋‹ค.
  4. 1์—์„œ 0์œผ๋กœ ์ด๋™ํ•œ ์†Œ ํ˜น์€ 0์—์„œ 1๋กœ ์ด๋™ํ•œ ์†Œ๋งŒ cnt๋ฅผ ์ฆ๊ฐ€ํ•˜๊ฒŒ ์„ค์ •ํ–ˆ๋‹ค. ์˜ˆ์ œ ์ž…๋ ฅ์„ ๋ณด๋ฉด ์†Œ๊ฐ€ 1 -> 0 -> 1 ์ด๋ ‡๊ฒŒ ์ด๋™ํ•˜์ง€ ์•Š๊ณ  1 -> 0 -> 0 -> 1 ํ˜น์€ 0 -> 1 -> 1 -> 0์œผ๋กœ ์ด๋™ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”๋กœ ๋‹ค์Œ์นธ์˜ ์›์†Œ๋ฅผ ํ™•์ธํ•˜๋Š” ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•ด๋„ ์˜ค๋‹ต ํŒ์ •์ด ์•ˆ ๋‚˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

 

from collections import defaultdict

n = int(input())
cows = defaultdict(list)

for i in range(1, 11):    # 1๋ฒˆ๋ถ€ํ„ฐ 10๋ฒˆ๊นŒ์ง€ ๋นˆ ๋ฆฌ์ŠคํŠธ ์„ ์–ธ
    cows[i] = []

for i in range(n):
    cows_num, cows_move = map(int, input().split())
    cows[cows_num].append(cows_move)    # ์†Œ์˜ ์œ„์น˜ ์ถ”๊ฐ€

cow_moves = [i for i in cows.values() if len(i) >= 2]    # 2๋ฒˆ์ด์ƒ ์›€์ง์ธ ์†Œ ๊บผ๋‚ด๊ธฐ    

cnt = 0
for i in range(len(cow_moves)):
    result = cow_moves[i]
    for j in range(len(result) - 1):
        if (not result[j] and result[j + 1]) or (result[j] and not result[j + 1]):    # 1 -> 0, 0 -> 1๋กœ ์ด๋™ํ•œ ์†Œ๋งŒ cnt ์ฆ๊ฐ€
            cnt += 1
print(cnt)
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€