백준 22115 c++ : DP, 배낭문제

magicdrill·2024년 12월 30일
0

백준 문제풀이

목록 보기
518/655

백준 22115 c++ : DP, 배낭문제

지정된 K를 정확하게 맞출 수 있는 커피잔의 최소 개수를 구하는 문제이다.
DP 계산할 때 현재 인덱스의 값과 한잔 더 마신 경우를 비교해서 최소값을 현재 인덱스의 값으로 최신화 한다?...

#include <iostream>
#include <vector>
#define MAX 2100000000

using namespace std;

void input_coffee(int* N, int* K, vector<int>& coffee) {
	cout << "\ninput_coffee()\n";
	int i, C;

	cin >> *N >> *K;
	for (i = 0; i < *N; i++) {
		cin >> C;
		coffee.push_back(C);
	}

	return;
}

int find_answer(int N, int K, vector<int>& coffee) {
	cout << "\nfind_answer()\n";
	int answer = 0;
	vector<int> DP(K + 1, MAX);
	int i, j, current_caffeine;

	//K만큼 채울수 있는 커피의 최소 개수
	DP[0] = 0;
	for (i = 0; i < N; i++) {
		current_caffeine = coffee[i];
		for (j = K; j >= current_caffeine; j--) {
			DP[j] = min(DP[j], DP[j - current_caffeine] + 1);
		}

		for (int temp : DP) {
			cout << temp << " ";
		}
		cout << "\n";
	}

	answer = (DP[K] == MAX) ? -1 : DP[K];

	return answer;
}

int main(void) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int N, K;
	vector<int> coffee;
	
	input_coffee(&N, &K, coffee);
	cout << find_answer(N, K, coffee) << "\n";

	return 0;
}

0개의 댓글