UVa 11517 - Exact Change

题目:给你一些钱币和一个价格,用钱币组成不小于价格的最低值,并使得此时使用的钱币总数最少。

分析:dp,01背包。初始化所有的的钱数组成都需要Max张钱币,F[0] = 0,更新即可。

因为,可以取超过price的值,所以将背包的容量扩大一些,找到最接近的即可。

说明:找零钱用背包。

#include <iostream>
#include <cstdlib>

using namespace std;

int F[20001];
int C[101];

int main()
{
	int t,p,n,sum;
	while (cin >> t)
	while (t --) {
		cin >> p >> n;
		sum = 0;
		for (int i = 0 ; i < n ; ++ i) {
			cin >> C[i];
			if (sum < p) sum += C[i];
		}

		for (int i = 1 ; i <= sum ; ++ i)
			F[i] = n;
		F[0] = 0;

		for (int i = 0 ; i < n ; ++ i)
		for (int j = sum ; j >= C[i] ; -- j)
			if (F[j] > F[j-C[i]]+1)
				F[j] = F[j-C[i]]+1;

		int s = p;
		while (s < sum && F[s] == n) s ++;

		cout << s << " " << F[s] << endl;
	}

	return 0;
}
时间: 2024-10-14 06:01:29

UVa 11517 - Exact Change的相关文章

POJ 2581 Exact Change Only(dp)

Language: Default Exact Change Only Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2584   Accepted: 883 Description Boudreaux reached over and shook awake Thibodeaux, who had dozed off somewhere in New Mexico. "Where we at?" Thibod

poj 2581 Exact Change Only (dp)

 Description Boudreaux reached over and shook awake Thibodeaux, who had dozed off somewhere in New Mexico. "Where we at?" Thibodeaux groggily yawned. "Not in Vegas, I gua-ran-tee, but could you get my knapsack?" Boudreaux asked, gest

HDU 1353 Exact Change Only

Exact Change Only Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 206    Accepted Submission(s): 64 Problem Description Boudreaux reached over and shook awake Thibodeaux, who had dozed off some

UVa 11057 - Exact Sum

题目:已知n个数a[1..n],还有另一个数M,在前n个数中找到差值最小的两个数使得他们的和是M. 分析:数学.排序,找M/2.然后向两边分别扩展即可. 设a[s]是第一个大于M/2的数字, 如果M是偶数,并且存在至少两个M/2,则a[s-1].a[s-2]一定是M/2: 否则a[s-1] <= M/2 ,a[s] > M,从这两点向两边扩展即可(l = s-1,r = s): 若a[l]+a[r] > M,l 向左移动(l --) 若a[l]+a[r] < M,r 向右移动(r

uva 674 Coin Change 经典dp入门

Coin Change Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Practice UVA 674 Appoint description: Description Download as PDF Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to m

UVA 674 Coin Change (DP)

Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money. For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent c

UVa 674 Coin Change (经典DP)

Coin Change Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money. For example

Exact Change

设计一个收银程序 checkCashRegister() ,其把购买价格(price)作为第一个参数 , 付款金额 (cash)作为第二个参数, 和收银机中零钱 (cid) 作为第三个参数. cid 是一个二维数组,存着当前可用的找零. 当收银机中的钱不够找零时返回字符串 "Insufficient Funds". 如果正好则返回字符串 "Closed". 否则, 返回应找回的零钱列表,且由大到小存在二维数组中. 当你遇到困难的时候,记得查看错误提示.阅读文档.搜索

FCC算法题--Exact Change

题目: 设计一个收银程序 checkCashRegister() ,其把购买价格(price)作为第一个参数 , 付款金额 (cash)作为第二个参数, 和收银机中零钱 (cid) 作为第三个参数. cid 是一个二维数组,存着当前可用的找零. 当收银机中的钱不够找零时返回字符串 "Insufficient Funds". 如果正好则返回字符串 "Closed". 否则, 返回应找回的零钱列表,且由大到小存在二维数组中. 当你遇到困难的时候,记得查看错误提示.阅读文