hdu 5616(dp)
2016-03-01 20:02
417 查看
Jam's balance
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 733 Accepted Submission(s): 351
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.
Input
The first line is a integer T(1≤T≤5),
means T test cases.
For each test case :
The first line is N,
means the number of weights.
The second line are N number,
i'th number wi(1≤wi≤100) means
the i'th weight's weight is wi.
The third line is a number M. M is
the weight of the object being measured.
Output
You should output the "YES"or"NO".
Sample Input
1 2 1 4 3 2 4 5
Sample Output
NO YES YES Hint For the Case 1:Put the 4 weight alone For the Case 2:Put the 4 weight and 1 weight on both side
Source
BestCoder Round #70
//好久没刷题了。。这题求的是给你N个数是否可以通过加减运算得到另外一个数
//主要利用类01背包的思想 dp[i][k] 为前i个数是否可以得到k 可以得到为1不能得到为0
//dp[i][k] = max(dp[i-1][k],dp[i-1][abs(k-a[i])]); dp[i][k] =max(dp[i][k],dp[i-1][k+a[i]])
#include <bits/stdc++.h> using namespace std; const int N=25; int arr ; int dp [N*100]; int main() { int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); for(int i=0;i<=n;i++) for(int k=0;k<N*100;k++) dp[i][k]=0; for(int i=1;i<=n;i++) { scanf("%d",arr+i); dp[i][0]=1; } dp[0][0]=1; for(int i=1;i<=n;i++) { for(int k=1;k<n*100;k++) { dp[i][k]=max(dp[i-1][k],dp[i-1][abs(k-arr[i])]); dp[i][k]=max(dp[i][k],dp[i-1][k+arr[i]]); } } int m; scanf("%d",&m); while(m--) { int val; scanf("%d",&val); if(dp [val]) puts("YES"); else puts("NO"); } } return 0; }
相关文章推荐
- AutoLayout
- 【POJ 2406】 Power Strings(KMP求循环节)
- mongodb 管理工具 for mac
- C++面向对象编程分享----20160301_李楚煌
- FMDB
- java获取泛型参数的类型的方法 - 实例讲解
- Swift的闭包
- Java实现几种常见排序算法代码
- 排列组合
- Leetcode 101 Symmetric Tree 二叉树
- spark MLlib
- 文件下载导致的 Broken pipe
- 用service还是thread?
- ArcGIS之二工程以及图层的建立
- StringBuffer StringBuilder线程安全的理解
- Hive 累积和的计算
- 欢迎使用CSDN-markdown编辑器
- 透过ATS缓存配置看如何判断HTTP资源是否可缓存方法论
- Poj 3320 Jessica's Reading Problem【动态尺取法】
- <iframe>内联框架