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

 

1495번: 기타리스트

첫째 줄에 N, S, M이 주어진다. (1 ≤ N ≤ 50, 1 ≤ M ≤ 1,000, 0 ≤ S ≤ M) 둘째 줄에는 각 곡이 시작하기 전에 줄 수 있는 볼륨의 차이가 주어진다. 이 값은 1보다 크거나 같고, M보다 작거나 같다.

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync('dev/stdin').toString().trim().split("\n");
const [N, S, M] = input.shift().split(" ").map(i => Number(i));
// 각 곡이 시작하기 전에 줄 수 있는 볼륨의 차이가 주어진다.
const volumes = input.shift().split(' ').map(i => Number(i));

// N: 곡의 개수
// S: 시작 볼륭
// M: 마지노선 볼륨 -> M보다 큰 값으로 볼륨을 바꿀 수 없다.

// 각 곡이 시작하기 전에 볼륨 바꿀 수 있음.
// V[i]는 i번째 곡을 연주하기 전에 바꿀 수 있는 볼륨을 의미
// 항상 리스트에 적힌 차이로만 볼륨을 바꿀 수 있다.
// 즉 현재 볼륨이 P이고 지금 i번재 곡을 연주하기 전이라면,
// i번째 곡은 P+V[i] 나 P-V[i]로 연주해야 한다.
// 0보다 작은 값으로 볼륨을 바꾸거나, M보다 큰 값으로 볼륨을 바꿀 수 없다.


// 3 5 10
// 5 3 7

let stack = [S];
let index = 0;

while(index < N) {
    if(stack.length === 0) {
        break;
    }
    let set = new Set(stack);
    const oldStack = [...Array.from(set)];
    stack = [];
    const len = oldStack.length;

    for(let i = 0 ; i < len ; i++) {
        const item = oldStack.pop();

        if(item + volumes[index] <= M) {
            stack.push(item + volumes[index]);
        }

        if(item - volumes[index] >= 0) {
            stack.push(item - volumes[index]);
        }
    }
    index++;
}

if(stack.length === 0) {
    console.log(-1);
} else {
    console.log(Math.max(...stack));
}
728x90

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

 

1271번: 엄청난 부자2

첫째 줄에는 최백준 조교가 가진 돈 n과 돈을 받으러 온 생명체의 수 m이 주어진다. (1 ≤ m ≤ n ≤ 101000, m과 n은 10진수 정수)

www.acmicpc.net

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

const n = BigInt(input[0]);
const m = BigInt(input[1]);

console.log((n/m).toString());
console.log((n%m).toString());
728x90

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

 

11724번: 연결 요소의 개수

첫째 줄에 정점의 개수 N과 간선의 개수 M이 주어진다. (1 ≤ N ≤ 1,000, 0 ≤ M ≤ N×(N-1)/2) 둘째 줄부터 M개의 줄에 간선의 양 끝점 u와 v가 주어진다. (1 ≤ u, v ≤ N, u ≠ v) 같은 간선은 한 번만 주

www.acmicpc.net

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

const [N, M] = input.shift().split(" ").map( i => Number(i));
const graph = new Array(N+1).fill(0).map(i => []);
let V = new Array(N+1).fill(false);

input.forEach((i) => {
    const [to, from] = i.split(" ").map(n => Number(n));
    graph[to].push(from);
    graph[from].push(to);
});

const dfs = (start) => {
    const visited = [];
    let stack = [start];

    while(stack.length) {
        const cur = stack.pop();
        if(!visited.includes(cur)) {
            visited.push(cur);
            V[cur] = true;
            stack.push(...graph[cur]);
            graph[cur].length = 0;
        }
    }
    return visited;
}

let start = 1;
let cnt = 0;

while(start !== graph.length) {
    if(graph[start].length) {
        dfs(start);
        cnt++;
    }
    start++;
}

V = V.filter(v => !v);
console.log(V.length > 1 ? cnt+V.length-1 : cnt);
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