HDU 5616 Jam ' s balance backpack DP

Source: Internet
Author: User

Jam ' s balance

problem DescriptionJim has a balance and N weights. (1≤N≤)
The balance can only tell whether things on different side is the same weight.
Weights can is put on the left side or right side arbitrarily.
Whether the balance can measure an object of weight M.

InputThe first line is a integerT(1≤t≤5) , means T test cases.
For each test case:
The first line isN, means the number of weights.
The second line isNnumber, I ' th numberWi(1≤wi≤) means the i ' th weight ' s weight isWi.
The third line is a number M. M is the weight of the object being measured.

OutputYou should output the "YES" or "NO".

Sample Input121 43245

Sample OutputNoyesyes HintFor the case 1:put the 4 weight alonefor the case 2:put the 4 weight and 1 weight on both side

Test Instructions:
Jam hasNn Weights and a balance without a cursor, now give him(1 \leq n \leq)(1≤n≤20) weights, weights can be placed on the left side, can also put on the right, asked to measure the weight asked, the number of questions is (1 \leq M \leq)(1≤m≤100) each.         
The following: This problem can be left, you can put on the right,n=20N=20 Obviously each state enumeration is not very realistic, because each group of weights can be turned into a lot of weight, of course, do not rule out someone messed up this problem, in fact, this problem is a greedy thought , we see WW is not big, so can use 01 backpack sweep once, of course, this is not enough, this can only put one side, considering that can be put on the other side, is can have reduced relations, so the reverse backpack again, pay attention to judge the boundary.
#include<bits/stdc++.h>usingnamespacestd; typedefLong LongLl;constintN = the;intn,m,a[n+Ten],dp[n+Ten],sum;void dp () {memset (DP,0, sizeof (DP)); dp[0]=1;  for(inti =1; i<= N; i++) {         for(intk=2; k;k--) for(intj = sum*2; j>=a[i];j--) {Dp[j]|=dp[j-a[i]]; }    }}intMain () {intt,x; scanf ("%d", &t);  while(t--) {scanf ("%d", &n); sum=0;  for(inti =1; I <= N; i++) scanf ("%d", &a[i]), sum+=a[i];    DP (); scanf ("%d", &m);  for(inti =1; i<= m; i++) {scanf ("%d", &x); intg = x+sum&&sum+x>=0&&dp[x+sum]; if(g) printf ("yes\n"); Elseprintf"no\n"); }} return0;}

HDU 5616 Jam ' s balance backpack DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.