题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5616
题目:
Jam's balance
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1810 Accepted Submission(s): 754
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
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.
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 int main(){ 6 int t,n,m; 7 int weight[205]; 8 int dp[20005]; 9 int sum; 10 scanf("%d",&t); 11 while (t--) { 12 scanf("%d",&n); 13 memset(dp, 0, sizeof(dp)); 14 dp[0]=1; 15 sum=0; 16 for (int i=0; i<n; i++) { 17 scanf("%d",&weight[i]); 18 sum+=weight[i]; 19 } 20 sort(weight, weight+n); 21 for (int i=0; i<n; i++) { 22 for (int j=sum; j>=0; j--) { 23 if(j>=weight[i])dp[j]=max(dp[j], dp[j-weight[i]]); 24 else dp[j]=max(dp[j], dp[weight[i]-j]); 25 } 26 } 27 scanf("%d",&m); 28 for (int i=0; i<m; i++) { 29 int w; 30 scanf("%d",&w); 31 printf("%s\n",dp[w]?"YES":"NO"); 32 } 33 } 34 return 0; 35 }