์ƒˆ์†Œ์‹

๐Ÿงฎ PS

๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜ (Greedy Algorithm) , ํƒ์š•

  • -

๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜(Greedy Algorithm)

 

๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ "๋งค ์„ ํƒ์—์„œ ๋‹น์žฅ ์ข‹์€๊ฒƒ๋งŒ์„ ์„ ํƒํ•ด ๋‚˜๊ฐ€๋Š” ๋ฐฉ๋ฒ•"์„ ๋œปํ•œ๋‹ค. ํŠน์ • ๊ธฐ์ค€์— ๋”ฐ๋ผ์„œ ์ข‹์€ ๊ฒƒ์„ ์„ ํƒํ•ด์•ผํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ์—์„œ ์–ด๋Š์ •๋„ ์ œ์‹œํ•ด์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ข‹๋‹ค, ๋‚˜์˜๋‹ค์˜ ๊ธฐ์ค€์„ ์„ธ์›Œ์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์„ž์—ฌ์„œ ๋‚˜์˜ค๋Š” ๋ฌธ์ œ๋“ค์ด ๋งŽ๋‹ค.

 

๊ฐ„๋‹จํ•œ ์˜ˆ์ œ (๊ฑฐ์Šค๋ฆ„ ๋ˆ)

 

๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋Œ€ํ‘œํ•˜๋Š” ๋ฌธ์ œ๋กœ '๊ฑฐ์Šค๋ฆ„ ๋ˆ' ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค.

์žํŒ๊ธฐ์— ๊ฑฐ์Šค๋ฆ„๋ˆ์„ ๋ฐฐ์ถœํ•ด์•ผ ํ•˜๋Š”๋ฐ , ์žํŒ๊ธฐ์—๋Š” 500, 100, 50, 10์›์งœ๋ฆฌ๊ฐ€ ๋ฌด์ˆ˜ํžˆ ์กด์žฌํ•œ๋‹ค. ์ด๋•Œ ๊ฑฐ์Šฌ๋Ÿฌ์ค„ ์ตœ์†Œ ๋™์ „์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ผ. (๋‹จ, ๊ฑฐ์Šค๋ฆ„ ๋ˆ์€ ํ•ญ์ƒ 10์˜ ๋ฐฐ์ˆ˜์ด๋‹ค.)

์ด๋Ÿฌํ•œ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๋ฉด, ๋ฌด์กฐ๊ฑด ํฐ ๋‹จ์œ„์˜ ๋™์ „(500์›)์œผ๋กœ ์ตœ๋Œ€ํ•œ ๊ฑฐ์Šฌ๋Ÿฌ์ฃผ๊ณ , ๊ทธ ๋‹ค์Œ ๊ฐ€๋Šฅํ•œ ํฐ ๋‹จ์œ„์˜ ๋™์ „(100์›)์„ ์„ ํƒํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

์œ„์˜ ์˜ˆ์ œ์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ๊ฑฐ์Šค๋ฆ„ ๋ˆ์ด ํ•ญ์ƒ 10์˜ ๋ฐฐ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์—, ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ ์šฉํ•ด๋„ ๋น—๋‚˜๊ฐˆ ์ผ์ด ์—†๋‹ค. ๊ทธ๋ž˜์„œ ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋งค์šฐ ํšจ๊ณผ์ ์ด์ง€๋งŒ, ๋‹ค๋ฅธ ๋ฌธ์ œ์— ๊ทธ๋ฆฌ๋””์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ ์šฉ์‹œํ‚ฌ๋•Œ๋Š” ์ด ๋ฐฉ๋ฒ•์ด ์ •๋‹นํ•œ์ง€, ์˜ˆ์™ธ๋Š” ์—†๋Š”์ง€ ๋ฐ˜๋“œ์‹œ ํ™•์ธํ•ด์•ผํ•œ๋‹ค.

๋‹ค์‹œ ๋งํ•˜๋ฉด, ๋งค ์„ ํƒ์˜ ์ˆœ๊ฐ„์— ๋Œ€ํ•ด์„œ๋Š” ์ตœ์ ์ด์ง€๋งŒ, ์ข…ํ•ฉ์ ์œผ๋กœ ๊ณ ๋ คํ•˜๋ฉด ์ตœ์ ์ด๋ผ๋Š” ๋ณด์žฅ์€ ์—†๋‹ค.

 

 

๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ๋ชฉ๋ก(๋ฐฑ์ค€)

 

14916 ๋ฒˆ : ๊ฑฐ์Šค๋ฆ„๋ˆ (๋‚œ์ด๋„ : ์‹ค๋ฒ„5)

https://www.acmicpc.net/problem/14916

๊ฑฐ์Šค๋ฆ„๋ˆ

n = int(input())
five = n // 5
two = 0
while (n > 0) :
    if ((n - (five * 5)) % 2 == 0):
        two = (n - (five * 5)) // 2
        answer = five + two
        break
    else : 
        five -= 1
        if five <= 0 :
            answer = -1
            break

print(answer)

 

2217๋ฒˆ : ๋กœํ”„ (๋‚œ์ด๋„ : ์‹ค๋ฒ„4)

https://www.acmicpc.net/problem/2217

๋กœํ”„

n = int(input())
arr = []
for i in range(n):
    arr.append(int(input()))
arr.sort(reverse=True)
solution=[]
for i in range(len(arr)):
    solution.append(arr[i] * (i+1))
print(max(solution))

 

 

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.