HDU5616 Jam's balance 动态规划
来源:互联网 发布:程序员的思维锻炼 编辑:程序博客网 时间:2024/05/16 11:55
Jam's balance
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 774 Accepted Submission(s): 374
Problem Description
Jim has a balance and N weights. (1≤N≤20)
The balance can only tell whether things on different side are the same weight.
Weights can be put on left side or right side arbitrarily.
Please tell whether the balance can measure an object of weight M.
The balance can only tell whether things on different side are the same weight.
Weights can be put on left side or right side arbitrarily.
Please tell whether the balance can measure an object of weight M.
Input
The first line is a integer T(1≤T≤5) , means T test cases.
For each test case :
The first line isN , means the number of weights.
The second line areN number, i'th number wi(1≤wi≤100) means the i'th weight's weight is wi .
The third line is a numberM . M is the weight of the object being measured.
For each test case :
The first line is
The second line are
The third line is a number
Output
You should output the "YES"or"NO".
Sample Input
121 43245
Sample Output
NOYESYESHintFor the Case 1:Put the 4 weight aloneFor the Case 2:Put the 4 weight and 1 weight on both side
分析:动态规划,对于前X个砝码,若能组合出质量为K的物品,则需要前X-1个砝码能组合出K-W[X]或K+W[X]或K的重量。
最后直接访问DP数组即可。
CODE:
//************************************************************************////*Author : Handsome How *////************************************************************************////#pragma comment(linker, "/STA CK:1024000000,1024000000")#pragma warning(disable:4996) #include <vector>#include <map>#include <set>#include <deque>#include <queue>#include <stack>#include <algorithm>#include <sstream>#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <cstring>#include <ctime> #include <cassert>#if defined(_MSC_VER) || __cplusplus > 199711L#define aut(r,v) auto r = (v)#else#define aut(r,v) __typeof(v) r = (v)#endif#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)#define fur(i,a,b) for(int i=(a);i<=(b);i++)#define furr(i,a,b) for(int i=(a);i>=(b);i--)#define cl(a) memset((a),0,sizeof(a))using namespace std;typedef long long LL;//----------------------------------------------------bool dp[25][2500];int a[30];int main(){ //freopen("E:\\data.in", "r", stdin); ios :: sync_with_stdio(false); int T; scanf("%d", &T); while (T--) { int n; scanf("%d", &n); fur(i, 1, n)scanf("%d", &a[i]); cl(dp); dp[0][0] = true; fur(i, 1, n)fur(j, 0, 2000) { if (j >= a[i]) if (dp[i - 1][j - a[i]]) dp[i][j] = 1; if (j + a[i] <= 2000) if (dp[i - 1][j + a[i]]) dp[i][j] = 1; if (dp[i - 1][j]) dp[i][j] = 1; } int q; scanf("%d", &q); while (q--) { int t; scanf("%d", &t); if (t > 2000) { printf("NO\n"); continue; } if (dp[n][t]) { printf("YES\n"); continue; } printf("NO\n"); } } return 0;}
0 0
- HDU5616 Jam's balance 动态规划
- hdu5616 Jam's balance
- HDU5616 Jam's balance
- 母函数解决 Jam's balance hdu5616
- HDU5616 Jam's balance[背包dp变形]
- hdu 5616 Jam's balance ( 动态规划 )
- hdu5616 Jam's math problem
- Jam's balance
- HDU 5616 Jam's balance
- hdu 5616 Jam's balance
- HDU 5616 Jam's balance
- HDU 5616 Jam's balance
- hdoj-5616-Jam's balance
- HDU 5616Jam's balance
- HDU 5616 Jam's balance
- HDOJ 5616 Jam's balance(母函数)
- hdoj 5616 Jam's balance 【母函数】
- HDOJ 5616-Jam's balance【模拟】
- Java中String s = new String("123")究竟创建了几个对象
- [高斯消元]UVA 12961 Lottery
- indexedDB 数据库
- 解决Android Activity切换时出现白屏问题
- web test LoadRunner xml / webservice / json / MQ / soapUI / baowen / WebService
- HDU5616 Jam's balance 动态规划
- (四)制作一个nginx 的Docker镜像
- 在Android6.0以后如何使用HTTPclient
- IOS中UINavigationBar的显示和隐藏
- 根据你的目的和意图来选择按钮样式
- 数学——Leftmost Digit
- 系统设计socket服务在电子商务上的试用
- web session详解
- Python编写定时关机小脚本