Coding Problem/백준

[백준] 13305 - 주유소

마탁이 2021. 5. 28. 20:17

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

 

13305번: 주유소

표준 입력으로 다음 정보가 주어진다. 첫 번째 줄에는 도시의 개수를 나타내는 정수 N(2 ≤ N ≤ 100,000)이 주어진다. 다음 줄에는 인접한 두 도시를 연결하는 도로의 길이가 제일 왼쪽 도로부터 N-1

www.acmicpc.net

  • 그리디 알고리즘을 활용할 수 있다면 간단히 해결할 수 있는 문제이다.
  • 현재 도시와 다음 도시들의 cost를 확인하며 loop를 이용해 계산한다.
더보기
N = int(input())
distList = list(map(int, str(input()).split()))
costList = list(map(int, str(input()).split()))


distIdx = 0
cityIdx = 0
nextCityIdx = 1
costSum = 0
while(True):
    if(distIdx >= len(distList)): break

    if(0 == distIdx):
        costSum += (costList[cityIdx] * distList[distIdx])
        distIdx += 1
        continue
    
    if(costList[cityIdx] <= costList[nextCityIdx]):
        costSum += (costList[cityIdx] * distList[distIdx])
        nextCityIdx += 1
    else:
        costSum += (costList[nextCityIdx] * distList[distIdx])
        cityIdx = nextCityIdx
        nextCityIdx += 1
    distIdx += 1

print(costSum)

'Coding Problem > 백준' 카테고리의 다른 글

[백준] 1010 - 다리놓기  (0) 2021.05.31
[백준] 1931 - 회의실 배정  (0) 2021.05.28
[백준] 1541 - 잃어버린 괄호  (0) 2021.05.28
[백준] 11047 - 동전 0  (0) 2021.05.28
[백준] 11399 - ATM  (0) 2021.05.28