Jam's balance set 暴力

Posted joeylee97

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jam's balance set 暴力相关的知识,希望对你有一定的参考价值。

Jim has a balance and N weights. (1N20)(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.

InputThe first line is a integer T(1T5)T(1≤T≤5), means T test cases. 
For each test case : 
The first line is NN, means the number of weights. 
The second line are NN number, i‘th number wi(1wi100)wi(1≤wi≤100) means the i‘th weight‘s weight is wiwi. 
The third line is a number MM. MM is the weight of the object being measured.
OutputYou 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
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<memory>
#include<bitset>
#include<string>
#include<functional>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#define MAXN 23
#define INF 0x3f3f3f3f
/*
给你一个数字M
+-x +-y 能否==M
*/
int a[MAXN], n, m;
set<int> s;
int main()
{
    int T, tmp;
    scanf("%d", &T);
    while (T--)
    {
        s.clear();
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        //int t1, t2;
        for (int i = 1; i <= n; i++)
        {
            set<int>::iterator e = s.end();
            vector<int> t;
            for (set<int>::iterator it = s.begin(); it != s.end(); it++)
            {
                if (!s.count(*it + a[i]))
                    t.push_back(*it + a[i]);
                if (!s.count(abs(*it - a[i])))
                    t.push_back(abs(*it - a[i]));
            }
            s.insert(a[i]);
            for (int i = 0; i < t.size(); i++)
                s.insert(t[i]);
        }
        scanf("%d", &m);
        while (m--)
        {
            scanf("%d", &tmp);
            if (s.count(tmp))
                printf("YES\n");
            else
                printf("NO\n");
        }
    }
}

 

以上是关于Jam's balance set 暴力的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5616 Jam's balance 背包DP

HDU 5616 Jam's balance(DP)

HDU 5616 Jam's balance(Jam的天平)

Jam's balance HDU - 5616 (01背包基础题)

hdu 5616 Jam's balance(dp 正反01背包)

HDU 5619 Jam's store