주유소

 

13305번: 주유소

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

www.acmicpc.net

 

내 코드

from sys import stdin

N = int(stdin.readline())
kmList = list(map(int, stdin.readline().split()))
oilList = list(map(int, stdin.readline().split()))
pay = 0
curIdx = 0

while True:
    # print(curIdx)
    if curIdx >= len(oilList)-1:
        break

    # 현재 주유소에서 나보다 주유소 가격이 싼경우 전까지 다 사야함
    nextIdx = curIdx + 1
    for idx in range(curIdx +1, len(oilList)):
        if idx == len(oilList) - 1:
            nextIdx = idx
        if oilList[idx] < oilList[curIdx]:
            nextIdx = idx
            break
    # print("nextId", nextIdx)

    # 구매한 km oil 계산
    for idx in range(curIdx, nextIdx):
        pay += oilList[curIdx] * kmList[idx]

    curIdx = nextIdx
    # print("pay", pay)
    # print()
print(pay)
728x90

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

 

13305번: 주유소

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

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().split("\n");

const N = input[0];
const km = input[1].split(" ").map( i => BigInt(i));
const cost = input[2].split(" ").map( i => BigInt(i));

let i = 0;
let current = cost[i];
let total = 0n;

while(true) {
    if(current >  cost[i]) {
        current =  cost[i];
    }
    total += current * km[i];
    i+=1;

    if(i === km.length) {
        break;
    }
}

console.log(String(total));

값이 크기 때문에 BigInt 를 쓰지 않으면 통과 하지 못함.

728x90

+ Recent posts