HDU 1085 Holding Bin-Laden Captive!(DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1085

解题报告:有1,2,5三种面值的硬币,这三种硬币的数量分别是num_1,num_2,num_5,问你不能凑的钱的最小值是多少。

DP,开一个这样的数组dp[i][3],然后dp[i][0]表示凑成 i 元钱需要dp[i][0]张1块的,需要dp[i][1]张两块的,dp[i][2]张5块的,然后依次往后递推,得到 i 的途径一共有三种,第一种是i-1加一张一块的,第二种是i-2加上1张两块的,第四种是i-5加上一张5块的,然后每次判断一下有没有超过张数就行了,如果发现这三种途径都不能凑成i,那么说明最小的不能凑的值就是i.

 1 #include<cstdio>
 2
 3 struct node
 4 {
 5     int a,b,c;
 6 }dp[8005];
 7 int main()
 8 {
 9     int n1,n2,n5;
10     while(scanf("%d%d%d",&n1,&n2,&n5),n1+n2+n5)
11     {
12         dp[0].a = 0;
13         dp[0].b = 0;
14         dp[0].c = 0;
15         int ans = 0;
16         for(int i = 1;i <= n1 + 2*n2+5*n5+1;++i)   //加1是在所有的都能实现的情况下可以直接得到比总数大1的结果
17         {
18             if(i >= 1 && dp[i-1].a+1 <= n1)
19             dp[i] = dp[i-1],dp[i].a += 1;
20             else if(i >= 2 && dp[i-2].b+1 <= n2)
21             dp[i] = dp[i-2],dp[i].b += 1;
22             else if(i >=5 && dp[i-5].c+1 <= n5)
23             dp[i] = dp[i-5],dp[i].c += 1;
24             else
25             {
26                 ans = i;
27                 break;
28             }
29         }
30         printf("%d\n",ans);
31     }
32     return 0;
33 }
34
35         

时间: 2024-10-12 08:10:48

HDU 1085 Holding Bin-Laden Captive!(DP)的相关文章

hdu 1085 Holding Bin-Laden Captive!(母函数)

http://acm.hdu.edu.cn/showproblem.php?pid=1085 题意:1元,2元,5元的硬币分别有num[1],num[2],num[3]个.问用这些硬币不能组合成的最小钱数. 继续母函数. 有两个注意的地方: 对c2[]初始化的同时也要对c1[]初始化. 最后枚举到sum+1,因为存在[1,sum]都可以凑成的可能,这时输出sum+1. #include <stdio.h> #include <iostream> #include <map&g

HDU 1085 Holding Bin-Laden Captive!(母函数或背包DP)

Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 23245    Accepted Submission(s): 10350 Problem Description We all know that Bin-Laden is a notorious terrorist, and he

HDU 1085 Holding Bin-Laden Captive!

Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Description We all know that Bin-Laden is a notorious terrorist, and he has disappeared for a long time. But recently, it is reported

HDU 1085 Holding Bin-Laden Captive!(母函数,或者找规律)

Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17653    Accepted Submission(s): 7902 Problem Description We all know that Bin-Laden is a notorious terrorist, and he h

hdu 1085 Holding Bin-Laden Captive! 母函数的基本运用,,还是不难的

Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16063    Accepted Submission(s): 7206 Problem Description We all know that Bin-Laden is a notorious terrorist, and he

HDOJ/HDU 1085 Holding Bin-Laden Captive!(非母函数求解)

Problem Description We all know that Bin-Laden is a notorious terrorist, and he has disappeared for a long time. But recently, it is reported that he hides in Hang Zhou of China! "Oh, God! How terrible! " Don't be so afraid, guys. Although he hi

HDU 1085 Holding Bin-Laden Captive! 活捉本拉登(AC代码)普通型母函数

题意:有面值分别为1.2.5的硬币,分别有num_1.num_2.num_5个,问不能组成的最小面值是多少?(0<=每种硬币个数<=1000,组成的面值>0) 思路:母函数解决.只有3个括号要作乘法,分别代表面值1.2.5所能组成的情况.需要两个数组,所能组成的最大值为num_1+2*num_2+5*num_5.如果在这个范围内都能组成,那么最小不能组成的面值为num_1+2*num_2+5*num_5+1.若没有1分钱的硬币,那么不能组成的肯定是1了. 数组的用法:ans[]保存第一个

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

HDU 1024 Max Sum Plus Plus --- dp+滚动数组

HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值,其中第i个子序列包括a[j], 则max(dp[m][k]),m<=k<=n 即为所求的结果 <2>初始状态: dp[i][0] = 0, dp[0][j] = 0; <3>状态转移: 决策:a[j]自己成为一个子段,还是接在前面一个子段的后面 方程: a[j]直接接在前面