DP还有很长很长一段路要走.. 题意:给出n纪念品的价格和钱数m,问最多能买多少件纪念品和买这些数量的纪念品的方案数. 首先,求能买最多的纪念品的数量,用贪心法可以解决.将价钱排序,然后从最便宜的开始买,这样就很容易求得最多买的纪念品的数量. 方案数就要用到动态规划. dp[j][k]表示花费不超过j元买k件物品的方案数 dp[j][k] += dp[j-a[i]][k-1] 因为这里本来是个三维数组的,多一个维度用来表示前i件物品.调整了循环顺序,类似01背包空间上的优化,所以倒着循环就可以利…
http://acm.hdu.edu.cn/showproblem.php?pid=2126 Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1904    Accepted Submission(s): 711 Problem Description When the winter holiday…
Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description When the winter holiday comes, a lot of people will have a trip. Generally, there are a lot of souvenirs to sell, and sometimes…
Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1662    Accepted Submission(s): 611 Problem Description When the winter holiday comes, a lot of people will have a trip. Genera…
When the winter holiday comes, a lot of people will have a trip. Generally, there are a lot of souvenirs to sell, and sometimes the travelers will buy some ones with pleasure. Not only can they give the souvenirs to their friends and families as gift…
Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1886    Accepted Submission(s): 699 Problem Description When the winter holiday comes, a lot of people will have a trip. Genera…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2157 题解: 给你一个有向图,n个节点m条边,问你从i到j共经过k个节点的方法数(不算i点). 题解: 先用邻接矩阵存图. 假设k = 2,那么从i到j的方法数 = ∑ way[i][x] * way[x][j] (0<=x<n && x!=i && x!=j) 诶?快看,那是矩阵乘法! 设邻接矩阵为A,若i到j有边则val[i][j] = 1. k = 2时答案矩…
题目描述 给定arr,arr中所有的值都为正数且不重复.每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个整数aim,求组成aim的方法数. 解题思路 完全背包 和"求换钱的最少张数"的转移来的状态完全相同. 初始化不同 转移方程为:dp[i][j]=dp[i-1][j]+dp[i][j-arr[i]] 代码 public class Solution { public int getMeansCnt(int arr[],int aim){ int[] dp=new i…
数组01背包. http://acm.hdu.edu.cn/showproblem.php?pid=2126 http://blog.csdn.net/crazy_ac/article/details/7869411 f[i][j][k]表示前i种物品,买了j个,花了小于等于k的钱的时候的方案数 因为是小于等于k,所以初始化的时候要注意哦. 那么转移的时候第i种物品取或者不取 f[i][j][k]+=f[i-1][j][k];   f[i][j][k]+=f[i-1][j-1][k-v[i]];…
Problem Description When the winter holiday comes, a lot of people will have a trip. Generally, there are a lot of souvenirs to sell, and sometimes the travelers will buy some ones with pleasure. Not only can they give the souvenirs to their friends…