ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python] ๋ฐฑ์ค€ 16206 ๋กค์ผ€์ดํฌ (Greedy)
    ๐Ÿ’ป Algorithm/Python 2022. 7. 8. 19:41

    ๐Ÿ“Œ ๋ฌธ์ œ

    ์˜ค๋Š˜์€ ์žฌํ˜„์ด์˜ ์ƒ์ผ์ด๋‹ค. ์žฌํ˜„์ด๋Š” ์นœ๊ตฌ N๋ช…์—๊ฒŒ ๋กค์ผ€์ดํฌ๋ฅผ 1๊ฐœ์”ฉ ์„ ๋ฌผ๋กœ ๋ฐ›์•˜๋‹ค. ๋กค์ผ€์ดํฌ์˜ ๊ธธ์ด๋Š” A1, A2, ..., AN์ด๋‹ค. ์žฌํ˜„์ด๋Š” ๊ธธ์ด๊ฐ€ 10์ธ ๋กค์ผ€์ดํฌ๋งŒ ๋จน๋Š”๋‹ค. ๋”ฐ๋ผ์„œ, ๋กค์ผ€์ดํฌ๋ฅผ ์ž˜๋ผ์„œ ๊ธธ์ด๊ฐ€ 10์ธ ๋กค์ผ€์ดํฌ๋ฅผ ์ตœ๋Œ€ํ•œ ๋งŽ์ด ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค.

     

    ๋กค์ผ€์ดํฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •์„ ํ†ตํ•ด์„œ ์ž๋ฅผ ์ˆ˜ ์žˆ๋‹ค.

    ์ž๋ฅผ ๋กค์ผ€์ดํฌ๋ฅผ ํ•˜๋‚˜ ๊ณ ๋ฅธ๋‹ค. ๊ธธ์ด๊ฐ€ 1๋ณด๋‹ค ํฐ ๋กค์ผ€์ดํฌ๋งŒ ์ž๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ, ๊ณ ๋ฅธ ๋กค์ผ€์ดํฌ์˜ ๊ธธ์ด๋ฅผ x๋ผ๊ณ  ํ•œ๋‹ค.
    0๋ณด๋‹ค ํฌ๊ณ , x๋ณด๋‹ค ์ž‘์€ ์ž์—ฐ์ˆ˜ y๋ฅผ ๊ณ ๋ฅธ๋‹ค.
    ๋กค์ผ€์ดํฌ๋ฅผ ์ž˜๋ผ ๊ธธ์ด๊ฐ€ y, x-y์ธ ๋กค์ผ€์ดํฌ ๋‘ ๊ฐœ๋กœ ๋งŒ๋“ ๋‹ค.
    ์žฌํ˜„์ด๋Š” ๋กค์ผ€์ดํฌ๋ฅผ ์ตœ๋Œ€ M๋ฒˆ ์ž๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ, ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ธธ์ด๊ฐ€ 10์ธ ๋กค์ผ€์ดํฌ ๊ฐœ์ˆ˜์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

    ์ž…๋ ฅ

    ์ฒซ์งธ ์ค„์— ๋กค์ผ€์ดํฌ์˜ ๊ฐœ์ˆ˜ N๊ณผ ์ž๋ฅผ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ํšŸ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N, M ≤ 1,000)

    ๋‘˜์งธ ์ค„์— ๋กค์ผ€์ดํฌ์˜ ๊ธธ์ด A1, A2, ..., AN์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ Ai ≤ 1,000)

    ์ถœ๋ ฅ

    ์žฌํ˜„์ด๊ฐ€ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ธธ์ด๊ฐ€ 10์ธ ๋กค์ผ€์ดํฌ ๊ฐœ์ˆ˜์˜ ์ตœ๋Œ“๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

    ์˜ˆ์ œ ์ž…๋ ฅ 1

    3 1
    10 10 10

    ์˜ˆ์ œ ์ถœ๋ ฅ 1

    3

    ์˜ˆ์ œ ์ž…๋ ฅ 2

    3 1
    10 10 20

    ์˜ˆ์ œ ์ถœ๋ ฅ 2

    4

    ์˜ˆ์ œ ์ž…๋ ฅ 3

    3 3
    20 20 20

    ์˜ˆ์ œ ์ถœ๋ ฅ 3

    6

    ์˜ˆ์ œ ์ž…๋ ฅ 4

    5 7
    10 20 30 40 50

    ์˜ˆ์ œ ์ถœ๋ ฅ 4

    11

    ์˜ˆ์ œ ์ž…๋ ฅ 5

    5 8
    34 45 56 12 23

    ์˜ˆ์ œ ์ถœ๋ ฅ 5

    8

     


    ๐Ÿ“Œ ํ’€์ด

    ๐Ÿ’ฌ Code

    import sys
    input = sys.stdin.readline
    
    n, limit = map(int, input().split())
    cake = [int(i) for i in input().split()]
    
    cakeTen = list(filter(lambda x: x % 10 == 0, cake))
    cakeNotTen = list(filter(lambda x: x % 10 != 0, cake))
    
    cnt = 0
    
    while limit > 0:
        if cakeTen:
            if cakeTen[0] // 10 - 1 > limit:
                cnt += limit
                limit = 0
            else:
                cnt += cakeTen[0] // 10
                limit -= (cakeTen[0] // 10 - 1)
            del cakeTen[0]
    
        elif cakeNotTen:
            if cakeNotTen[0] // 10 > limit:
                cnt += limit
                limit = 0
            else:
                cnt += cakeNotTen[0] // 10
                limit -= cakeNotTen[0] // 10
            del cakeNotTen[0]
    
        else:
            break
    
    print(cnt)

    ๐Ÿ’ก Solution


    ์ผ€์ดํฌ๋Š” ์œ„์™€ ๊ฐ™์ด ์„ธ๊ฐ€์ง€ ๊ฒฝ์šฐ๋กœ ๋‚˜๋ˆ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๊ธธ์ด 10์€ ํ•œ ๋ฒˆ๋„ ์•ˆ ์ž๋ฅด๊ณ  ์ผ€์ดํฌ๋ฅผ ์–ป๋Š”๋‹ค๋Š” ์ ์—์„œ ๋”ฐ๋กœ ๋ถ„๋ฅ˜ํ–ˆ์ง€๋งŒ 30๊ณผ ํ•˜๋‚˜์˜ ๊ฒฝ์šฐ๋กœ ๋ฌถ์„ ์ˆ˜ ์žˆ๋Š”๋ฐ,

     

    • ๊ธธ์ด๊ฐ€ 10์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ๊ฒฝ์šฐ: (๊ธธ์ด÷10)-1๋ฒˆ ์ž๋ฅด๊ณ  (๊ธธ์ด÷10)๊ฐœ ํš๋“
    • ๊ธธ์ด๊ฐ€ 10์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€์ง€ ์•Š๋Š” ๊ฒฝ์šฐ: (๊ธธ์ด÷10)๋ฒˆ ์ž๋ฅด๊ณ  (๊ธธ์ด÷10)๊ฐœ ํš๋“

     

    ์š”๋ ‡๊ฒŒ ๋‚˜๋ˆ„๋ฉด ๋ฉ๋‹ˆ๋‹ค.

    cake = [int(i) for i in input().split()]
    
    cakeTen = list(filter(lambda x: x % 10 == 0, cake))
    cakeNotTen = list(filter(lambda x: x % 10 != 0, cake))

    ๋”ฐ๋ผ์„œ lambda๋ฅผ ์ด์šฉํ•ด ๊ธธ์ด๊ฐ€ 10์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ผ€์ดํฌ(cakeTen)์™€ ๋–จ์–ด์ง€์ง€ ์•Š๋Š” ์ผ€์ดํฌ(cakeNotTen)๋ฅผ ๋‹ค๋ฅธ ๋ฆฌ์ŠคํŠธ์— ๋ถ„๋ฅ˜ํ•ด ๋‹ด์•„์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

     

    ์ปคํŒ…ํšŸ์ˆ˜๋Š” ์ •ํ•ด์ ธ ์žˆ๋Š”๋ฐ ์ผ€์ดํฌ ๊ฐฏ์ˆ˜๋ฅผ ์ตœ๋Œ€๋กœ ํš๋“ํ•ด์•ผ ํ•˜๋ฏ€๋กœ, ์ปคํŒ…ํšŸ์ˆ˜๊ฐ€ ๋” ์ ์€ cakeTen์„ ์šฐ์„ ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    cnt = 0
    
    while limit > 0:
        # 10์˜ ๋ฐฐ์ˆ˜ ์ผ€์ดํฌ๊ฐ€ ๋‚จ์•„์žˆ์œผ๋ฉด
        if cakeTen:
            # ์ผ€์ดํฌ๊ฐ€ ๊ธธ์–ด์„œ ๋งŽ์ด ์ž๋ฅผ ์ˆ˜ ์žˆ๋Š”๋ฐ ์ปคํŒ…ํšŸ์ˆ˜ ์ œํ•œ์— ๊ฑธ๋ฆฌ๋Š” ๊ฒฝ์šฐ
            if cakeTen[0] // 10 - 1 > limit:
                cnt += limit
                limit = 0
            else:
                cnt += cakeTen[0] // 10
                limit -= (cakeTen[0] // 10 - 1)
            del cakeTen[0]
    
        # 10์˜ ๋ฐฐ์ˆ˜ ์ผ€์ดํฌ๊ฐ€ ์—†์œผ๋ฉด
        elif cakeNotTen:
            # ์ผ€์ดํฌ๊ฐ€ ๊ธธ์–ด์„œ ๋งŽ์ด ์ž๋ฅผ ์ˆ˜ ์žˆ๋Š”๋ฐ ์ปคํŒ…ํšŸ์ˆ˜ ์ œํ•œ์— ๊ฑธ๋ฆฌ๋Š” ๊ฒฝ์šฐ
            if cakeNotTen[0] // 10 > limit:
                cnt += limit
                limit = 0
            else:
                cnt += cakeNotTen[0] // 10
                limit -= cakeNotTen[0] // 10
            del cakeNotTen[0]
    
        # ๋ชจ๋“  ์ผ€์ดํฌ๋ฅผ ๋‹ค ํƒ์ƒ‰ํ–ˆ์œผ๋ฉด
        else:
            break

     

     

     

    ๋Œ“๊ธ€

Designed by Tistory.