๐Ÿ˜Š ๋ฐฑ์ค€ 13305 : ์ฃผ์œ ์†Œ

3Juhwanยท2021๋…„ 2์›” 27์ผ
0

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
14/23

13305: ์ฃผ์œ ์†Œ

Greedy ๋„ค ๋ฒˆ์งธ ๋ฌธ์ œ~!
์ด๊ฒƒ๋„ ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€๋‹ค.


๐Ÿ“Œ Try 1

import sys
sys.setrecursionlimit(10000000)

def next(curIdx, nextIdx, length):
    if nextIdx >= length:
        return 0
    
    curCost, nextCost = cost[curIdx], cost[nextIdx]
    curDis = distance[nextIdx - 1]
    Cost = curCost * curDis
    
    # ํ˜„์žฌ ๊ฐ’์ด ๋” ์ €๋ ดํ•  ๋•Œ
    if curCost < nextCost:
        return Cost + next(curIdx, nextIdx + 1, length)
    else:
        return Cost + next(nextIdx, nextIdx + 1, length)

N = int(input())
distance = list(map(int, input().split()))
cost = list(map(int, input().split()))

sumCost = next(0, 1, len(cost))
print(sumCost)

์žฌ๊ท€๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์—ˆ๋‹ค.
N์˜ ๋ฒ”์œ„๊ฐ€ ๋งค์šฐ ํฌ๊ธฐ ๋•Œ๋ฌธ์— ์žฌ๊ท€ depth๋„ ๋งค์šฐ ํฌ๊ฒŒ ์žก์•˜๋‹ค.

์ •๋‹น์„ฑ ๋ถ„์„ :

ํ˜„์žฌ ๋„์‹œ์˜ ๊ธฐ๋ฆ„ ๊ฐ’๋ณด๋‹ค ๊ธฐ๋ฆ„ ๊ฐ’์ด ์ €๋ ดํ•œ ๋„์‹œ์— ๋„์ฐฉํ•˜๊ธฐ ์ „๊นŒ์ง€ ํ•„์š”ํ•œ ๊ธฐ๋ฆ„์„ ํ˜„์žฌ ๋„์‹œ์—์„œ ์ฑ„์šฐ๋ฉด ์ตœ์†Œ ๊ฐ€๊ฒฉ์œผ๋กœ ๋„์ฐฉํ•  ์ˆ˜ ์žˆ๋‹ค.


๐ŸŽฏ ๋‹ค๋ฅธ ๋ถ„ ์ฝ”๋“œ

N = int(input())
distance = list(map(int, input().split()))
cost = list(map(int, input().split()))
won = 0

h = cost[0]
for i in range(len(distance)):
    if cost[i] <= h:
        h = cost[i]
    won += h * distance[i]

print(won)

Other

min ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค.


๐ŸŽ Reference

profile
Codeforces์™€ USACO ํ’€์ด๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ๊ธ€๋„ ๊ณ„์† ์—…๋ฐ์ดํŠธ ๋ฉ๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€