The Values You Can Make
来源:互联网 发布:酒店清洗床单 知乎 编辑:程序博客网 时间:2024/06/05 03:17
题目链接:http://codeforces.com/problemset/problem/687/C
题意分析:给定n个数,用这n个数在和为k的情况下从n个数中找到所有的value,这些value之和也为k.
DP题我练的真的太少了,总是写不出状态状态方程,下一步打算好好刷刷紫书第八章的题目。
不能因为不擅长就逃避它们,感谢小伙伴给我讲这道题。定义数组dp[i][j] = 1表示由j 可以组成i
若dp[i][j] == 1则dp[i + c][j] = 1 dp[i + c][j + c] = 1; (c为常数)由此确立状态转移方程。
#include<iostream>#include<algorithm>#include<cstring>#include<iomanip>#include<cctype>#include<string>#include<cmath>#include<cstdio>#include<vector>#include<cstdlib>#define LL long longtypedef long long ll;using namespace std;const int maxn = 510;//ll gcd(ll a, ll b)//{// return !b ? a : gcd(b, a % b);//}//ll lcm(ll a, ll b)//{// return a / gcd(a, b) * b;//}vector<int> ans;int dp[maxn][maxn];int main(){ int n, k; while(scanf("%d%d",&n, &k) == 2) { int x; dp[0][0] = 1; for(int i = 0; i < n; i++) { scanf("%d",&x);//输入x后判断它能否和其他数组成k(x必须小于等于k才有可能成立) for(int j = k; j >= x; j--) { for(int l = 0; l + x <= k; l++) { if(dp[j - x][l]) { dp[j][l] = 1; dp[j][l + x] = 1; } } } } for(int i = 0; i <= k; i++) { if(dp[k][i]) ans.push_back(i); } printf("%d\n",ans.size()); for(int i = 0; i < ans.size(); i++) { if(i) printf(" "); printf("%d",ans[i]); } printf("\n"); } return 0;}
0 0
- The Values You Can Make
- The Values You Can Make
- 687C: The values you can make
- The Values You Can Make CodeForces
- 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 689 C The Values You Can Make(dp)
- codeforces 688 E. The Values You Can Make
- CodeForces 687C The Values You Can Make
- cf--E. The Values You Can Make(dp)
- 【43.75%】【codeforces 688E】The Values You Can Make
- Codeforces 688E The Values You Can Make【Dp】
- Codeforces 687C The Values You Can Make DP
- Codeforces 688E The Values You Can Make(DP)
- 动态规划—— E. The Values You Can Make
- Codeforces Round #360 (Div. 2) E The Values You Can Make(DP)
- 【MyBatis框架】MyBatis入门程序第一部分
- 安卓中的Flux架构
- Turtle随机生成图像
- Android使用intent跳转到其它应用activity界面
- Leetcode no. 41
- The Values You Can Make
- LRU Cache
- Android启动过程深入解析
- 树状数组----构建与基本操作
- Balanced Lineup(线段树——根据区间找最值)
- UITableView错题集
- m_pDC->GetSafeHdc()的思考
- mysql++ 官方文档阅读简略摘要
- PHP调用OpenOffice实现word转PDF