티스토리 뷰
https://www.acmicpc.net/problem/19942
19942번: 다이어트
식재료 N개 중에서 몇 개를 선택해서 이들의 영양분(단백질, 탄수화물, 지방, 비타민)이 일정 이상이 되어야 한다. 아래 표에 제시된 6가지의 식재료 중에서 몇 개를 선택해서 이들의 영양분의 각
www.acmicpc.net
const fs = require('fs');
const input = fs.readFileSync("./dev/stdin").toString().trim().split("\n");
function main(input) {
const N = +input.shift()
const food = input.map(v => v.split(' ').map(Number))
const [mp, mf, ms, mv] = food.shift()
let answer = Infinity;
let selectedFoods = [];
for (let k = 1; k < (1 << N); k++) {
let p = 0;
let f = 0;
let s = 0;
let v = 0;
let cost = 0;
let temp = []
for (let i = 0; i < N; i++) {
if (k & (1 << i)) {
p += food[i][0]
f += food[i][1]
s += food[i][2]
v += food[i][3]
cost += food[i][4]
temp.push({ index: i + 1, cost: food[i][4] })
}
}
if (p >= mp && f >= mf && s >= ms && v >= mv) {
if (cost <= answer) {
if (cost < answer) selectedFoods = [];
answer = cost
const f = temp.sort((a, b) => {
a.cost - b.cost
}).map(v => v.index).join(' ')
selectedFoods.push(f);
};
}
}
if (answer == Infinity) {
console.log(-1)
} else {
console.log(answer)
console.log(selectedFoods.sort()[0])
}
}
main(input);
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 24078번: 余り (Remainder) (0) | 2022.04.01 |
---|---|
Node.js)백준 1285: 동전 뒤집기 (0) | 2022.04.01 |
Node.js)백준 15684번: 사다리 조작 (0) | 2022.03.31 |
Node.js) 백준 9934번: 완전 이진 트리 (1) | 2022.03.31 |
Node.js) 백준 3197번: 백조의 호수 (0) | 2022.03.31 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 투포인터
- create db
- 동적프로그래밍
- 서버점검
- 최소공통조상
- 은둔청년체험
- MySQL
- 다이나밍프로그래밍
- 그래프
- DB 생성
- BFS
- MOD
- node.js
- 다이나믹프로그래밍
- 롱베케이션
- create databases;
- KMP
- 로드나인
- 서버개발
- 투포인터 연습
- 개발자면접
- 면접질문
- 면접비
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
글 보관함