7.6 MCodeForces 687CThe Values You Can Make
来源:互联网 发布:淘宝推广赚佣金 编辑:程序博客网 时间:2024/05/18 03:44
题意:
给你n个面值不同的硬币,问买下价值为k的巧克力后,剩下的硬币价值有多少种情况,并列举出来。
思路:
用动态规划。
代码:
#include <iostream>#include <vector>using namespace std;bool dp[501][501][501];//表示在第i个数时,和为j时,能否构造出kvector<int> vi;int main(){ ios::sync_with_stdio(0); int n,k; cin>>n>>k; dp[0][0][0]=1; for(int i=1;i<=n;i++){ int c; cin>>c; for(int j=0;j<=k;j++) for(int p=0;p<=j;p++){ dp[i][j][p] |=dp[i-1][j][p];//第i个数未算入和中 if(j>=c) dp[i][j][p] |= dp[i-1][j-c][p];//第i个数算入和中,但不参与构造p if(p>=c) dp[i][j][p] |= dp[i-1][j-c][p-c];//第i个数算入和中,参与构造p } } for(int i=0;i<=k;i++)if(dp[n][k][i]){ vi.push_back(i); } cout<<vi.size()<<endl; for(int i=0;i<vi.size();i++){ if(i!=vi.size()-1)cout<<vi[i]<<' '; else cout<<vi[i]<<endl; } return 0;}
0 0
- 7.6 MCodeForces 687CThe Values You Can Make
- 687C: The values you can make
- The Values You Can Make
- The Values You Can Make
- Codeforces 687C The Values You Can Make(背包)
- Codeforces-687C The Values You Can Make
- CodeForces 687C The Values You Can Make
- CodeForces 687C The Values You Can Make
- CodeForces 687C The Values You Can Make
- Codeforces 687C The Values You Can Make DP
- The Values You Can Make CodeForces
- 7_6_M题 The Values You Can Make题解[Codeforces 687C](DP)
- CodeForces 687C - The Values You Can Make(01背包dp)
- codeforces 687C - The Values You Can Make(背包+滚动数组)
- [DP] Codeforces 687C #360 (Div. 1) C. The Values You Can Make
- Codeforces 689 C The Values You Can Make(dp)
- codeforces 688 E. The Values You Can Make
- cf--E. The Values You Can Make(dp)
- CSS居中和多列布局
- leetcode 32. Longest Valid Parentheses __ java
- Rsync的其他几个常见参数
- 罗马数字
- Platt SMO 支持向量机算法(Python实现)
- 7.6 MCodeForces 687CThe Values You Can Make
- 通过多项式来拟合股票收盘价的趋势图
- Intellij Idea基于现有的Eclipse工程创建 多模块工程--spring spring mvc hibernate - SSH- resin
- (一) linux内核基础
- MySQL安装目录缺少data文件夹解决方法
- hadoop常用命令
- The JSP specification requires that an attribute name is preceded by whitespace
- Android::开放root权限
- cpu字节序(整理)