码迷,mamicode.com
首页 > 其他好文 > 详细

Jam's balance set 暴力

时间:2017-08-19 11:13:15      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:sid   blog   etc   div   ram   map   func   mat   xmlns   

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 暴力

标签:sid   blog   etc   div   ram   map   func   mat   xmlns   

原文地址:http://www.cnblogs.com/joeylee97/p/7395475.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!