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?" Thibodeaux groggily yawned.

"Not in Vegas, I gua-ran-tee, but could you get my knapsack?" Boudreaux asked, gesturing to the worn, leather backpack in the back seat of their cherry red Ford Miata.

"Why, is there a problem?"

"Just hand me my knapsack, problem or not."

Thibodeaux complied, glancing up as Boudreaux slowed the car to a stop in a line of vehicles approaching a toll booth. "$1.65 -- Exact change only," Thibodeaux read the yellow sign on the front of a small wooden building occupied by a lone toll booth operator.
"I have to get $1.65 in exact change?" Thibodeaux asked, digging through the knapsack, "all I have are ten quarters, four dimes, and three pennies. I don‘t have any nickels . . ."

"Just give me five of the quarters and the four dimes," Boudreaux replied, holding out his hand.

"Oh yeah," Thibodeaux said, handing over the coins, "that does add up to $1.65. I wish there were an easy way to figure out if you have an exact monetary amount, given a set of coins."

"Hmmm," Boudreaux shrugged, "sounds like a good programming problem."

Input

Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets.

A single data set has 1 component:

Start line - A single line:

A B C D E

where:

A: (0.01 <= A <= 5.00) is a decimal number (to two decimal places) of a monetary amount.

B: (0 <= B <= 100) is an integer number of quarters (one quarter = $0.25).

C: (0 <= C <= 100) is an integer number of dimes (one dime = $0.10).

D: (0 <= D <= 100) is an integer number of nickels (one nickel = $0.05).

E: (0 <= E <= 100) is an integer number of pennies (one penny = $0.01).

Output

For each data set, there will be exactly one line of output. If there exists one or more subsets of the given coins whose values add up to the given monetary amount exactly, the output will be a single line in the form:

A B C D

where A is the number of quarters, B is the number of dimes, C is the number of nickels, and D is the number of pennies, for the subset with the fewest number of coins. Otherwise, the output will be a single line with the statement:

NO EXACT CHANGE

Sample Input

0.45 2 1 1 4
0.75 3 7 1 75

Sample Output

NO EXACT CHANGE
3 0 0 0

Source

South Central USA 2003

就是把钱放大100倍后看组合成all,种钱最少需要几张钱?

dp+记录路径,值得一提的是这个题也可以4个for循环过,但是还是感觉dp高大上一点吧

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 1000

int num[5],dp[N],pre[N],all;
int op[5]={25,10,5,1};
int ans[N];

int main()
{
	int i,j;
	double x;
	while(~scanf("%lf",&x))
	{
		all=(int)(x*100);
		for(i=0;i<4;i++)
			scanf("%d",&num[i]);

		memset(dp,0,sizeof(dp));
		dp[0]=1;

		for(i=0;i<4;i++)
		{
			if(op[i]*num[i]>=all)
			{
				for(int v=op[i];v<=all;v++)
					if(dp[v-op[i]]&&(dp[v]==0||dp[v]>dp[v-op[i]]+1))
				{
					dp[v]=dp[v-op[i]]+1;
					pre[v]=v-op[i];
				}
			}
			else
			{
				int t=num[i];
				while(t--)
				{
					for(int v=all;v>=op[i];v--)
						 if(dp[v-op[i]]&&(dp[v]==0||dp[v]>dp[v-op[i]]+1))
						{
							dp[v]=dp[v-op[i]]+1;
							pre[v]=v-op[i];
						}
				}
			}
		}
		if(!dp[all])
		{
			printf("NO EXACT CHANGE\n");
			continue;
		}
		 memset(ans,0,sizeof(dp));
		 i=all;
		 while(i)
		 {
		 	ans[i-pre[i]]++;
		 	i=pre[i];
		 }
		 printf("%d %d %d %d\n",ans[25],ans[10],ans[5],ans[1]);
	}
	return 0;
}
时间: 2024-08-26 03:54:30

POJ 2581 Exact Change Only(dp)的相关文章

【POJ 1191】 棋盘分割(DP)

[POJ 1191] 棋盘分割(DP) Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13811   Accepted: 4917 Description 将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的部分继续如此分割,这样割了(n-1)次后,连同最后剩下的矩形棋盘共有n块矩形棋盘.(每次切割都只能沿着棋盘格子的边进行) 原棋盘上每一格有一个分值,一块矩形棋盘的总分为其所含各格分

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

poj 3267 The Cow Lexicon (dp)

链接:poj 3267 题意:给定一个主串,和单词序列,问最少在主串删除多少字母, 可以使其匹配到单词序列,如 browndcodw cow milk white black brown farmer 删除主串中的两个d,brown和cow就与整个主串匹配了 分析:dp[i]表示从主串中第i个字符开始,到第L个字符(结尾处) 这段区间最少要删除的字符数, 则状态转移方程为: dp[i]=dp[i+1]+1  不能匹配时 dp[i]=min(dp[i],dp[pos]+pos-i-m)  可以匹配

poj - 1953 - World Cup Noise(dp)

题意:n位长的01序列(0 < n < 45),但不能出现连续的两个1,问序列有多少种. 题目链接:http://poj.org/problem?id=1953 -->>设dp[i][j]表示前 i 位中第 i 位为 j 时的序列数,则状态转移方程为: dp[i][0] = dp[i - 1][0] + dp[i - 1][1]; dp[i][1] = dp[i - 1][0]; 因为对于相同的n,其结果是固定的,所以可以对一个n只计算一次,然后记住她.. #include <

poj - 1050 - To the Max(dp)

题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 -->>将二维压缩为一维,对一维进行dp求解. 将二维压缩成一维: 1.第1行 2.第2行加第1行 3.第3行加第2行加第1行 -- N.第N行加第N-1行加--加第1行 1.第2行 2.第3行加第2行 -- 1.第N行 对于一维情况,设dp[i]表示以第i个元素结尾的最大连续和,则状态转移方程为

POJ 3486 &amp; HDU 1913 Computers(dp)

题目链接:PKU:HDU: PKU:http://poj.org/problem?id=3486 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1913 Description Everybody is fond of computers, but buying a new one is always a money challenge. Fortunately, there is always a convenient way to deal wi

POJ 3666 Making the Grade (DP)

题意:输入N, 然后输入N个数,求最小的改动这些数使之成非严格递增即可,要是非严格递减,反过来再求一下就可以了. 析:并不会做,知道是DP,但就是不会,菜....d[i][j]表示前 i 个数中,最大的是 j,那么转移方程为,d[i][j] = abs(j-w[i])+min(d[i-1][k]);(k<=j). 用滚动数组更加快捷,空间复杂度也低. 代码如下: #include <cstdio> #include <string> #include <cstdlib&

POJ 3666 Making the Grade(DP)

题目链接:点击打开链接 题意:给n个数, 要求把这个数列变成非减或者非增数列, 求最小该变量之和. 思路:可以这样设计DP, d[i][j]表示第i个数变成j的最优解, 这样它转移到d[i-1][k], 其中k<=j, 这是变成上升的, 代价是abs(a[i] - j). 但是数太大了, 又因为每个数肯定会变成这些数中的一个数会最优, 所以我们不妨将n个数先离散化一下, 这样状态就表示成d[i][j]表示第i个数变成第j小的数, 转移到d[i-1][k],其中k<=j. 但是这样还是超时了,

poj 2533 Longest Ordered Subsequence(dp)

Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36159   Accepted: 15882 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ...