ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 12970 AB with Python
    PS 2022. 5. 9. 20:52
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 12970 AB

    ๐Ÿ’ก ์กฐ๊ฑด

    1. ๋ฌธ์ž์—ด์˜ ๊ธธ์ด n

    2. 0 โ‰ค i < j < N ์ด๋ฉด์„œ s[i] == 'A' && s[j] == 'B'๋ฅผ ๋งŒ์กฑํ•˜๋Š” (i, j) ์Œ์˜ ๊ฐœ์ˆ˜ K ๊ฐœ๊ฐ€ ์žˆ๋‹ค.

    3. N๊ณผ K๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (2 โ‰ค N โ‰ค 50, 0 โ‰ค K โ‰ค N(N-1)/2)

    4. ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ฌธ์ž์—ด S๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๊ฐ€๋Šฅํ•œ S๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๋ผ๋ฉด, ์•„๋ฌด๊ฑฐ๋‚˜ ์ถœ๋ ฅํ•œ๋‹ค.

    5. S๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

    6. ์ˆ˜ํ•™, ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œ ํ˜•์˜ ๋ฌธ์ œ

    ๐Ÿ–ฅ ์†Œ์Šค ์ฝ”๋“œ

    from sys import stdin
    n, k = map(int, stdin.readline().split())
    
    
    def solve(n, k):
        s = list('B' * n)
        acnt, curk, lidx = 0, 0, -1
    
        while curk < k:
            if lidx <= acnt - 1:
                if s[n - 1 - (acnt + 1)] == 'A':
                    break
    
                s[n - 1 - (acnt + 1)] = 'A'
                lidx = n - 1 - (acnt + 1)
                acnt += 1
                curk += 1
            else:
                s[lidx] = 'B'
                s[lidx - 1] = 'A'
                lidx -= 1
                curk += 1
    
        return s if curk == k else '-1'
    
    
    answer = solve(n, k)
    print(*answer, sep='')
    

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    3 2

    ์‹คํ–‰๊ฒฐ๊ณผ

    ABB

    โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

    1. ๋ฌธ์ž์—ด s๋Š” B๋กœ n๋งŒํผ ์ฑ„์›Œ์„œ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

    2. ์˜ˆ์ œ 4๋ฒˆ์„ ์˜ˆ๋กœ ๋“ค๋ฉด, BBBBBBBBBB ์œผ๋กœ ๋จผ์ € s๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

    3. ์˜ค๋ฅธ์ชฝ ๊ธฐ์ค€ ๋‘๋ฒˆ์งธ ๋ถ€ํ„ฐ A๋ฅผ ๋„ฃ๋Š”๋ฐ, ๋์— ๋„ฃ์–ด๋ด์•ผ ์Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 0์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‘๋ฒˆ์งธ๋ถ€ํ„ฐ ๋„ฃ๋Š”๋‹ค.

    4. A๋ฅผ ์™ผ์ชฝ์œผ๋กœ ๋ฐ€๋ฉด์„œ ์Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ฒ€์‚ฌํ•œ๋‹ค.

    5. curk ์˜ ๊ฐ’์ด k๊ฐ€ ๋˜์—ˆ์„ ๋•Œ, while ์„ ์ข…๋ฃŒํ•˜๊ณ , ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•ด์ค€๋‹ค.

    6. ๋งŒ์•ฝ while ์ด ์ข…๋ฃŒ๋œ ํ›„์—๋„ curk ๊ฐ’์ด k ์˜ ๊ฐ’๊ณผ ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด, ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์—†๋Š” ๊ฒƒ์ด๋‹ˆ -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

    ๐Ÿ’พ ๋Š๋‚€์ 

    1. ๊ทธ๋ฆฌ๋””๋Š” ๋„ˆ๋ฌด ์–ด๋ ต๋‹ค.
    2. ๋ฌธ์ œ๊ฐ€ ์กฐ๊ธˆ ๋‚œ์ด๋„๊ฐ€ ๋†’์•„์ง€๋ฉด ์†์„ ๋Œ€์ง€ ๋ชปํ•˜๋Š” ์ƒํ™ฉ์ด ์˜ค๋Š” ๊ฒƒ ๊ฐ™๋‹ค.
    3. ๊ทธ๋ฆฌ๋”” ์œ ํ˜•์„ ๋”์šฑ ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.