Jim has a balance and N weights. (1≤N≤20)(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(1≤T≤5)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(1≤wi≤100)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. 1
  2. 2
  3. 1 4
  4. 3
  5. 2
  6. 4
  7. 5

Sample Output

  1. NO
  2. YES
  3. YES

Hint

  1. For the Case 1:Put the 4 weight alone
  2. For the Case 2:Put the 4 weight and 1 weight on both side
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<sstream>
  6. #include<algorithm>
  7. #include<queue>
  8. #include<vector>
  9. #include<cmath>
  10. #include<map>
  11. #include<stack>
  12. #include<set>
  13. #include<memory>
  14. #include<bitset>
  15. #include<string>
  16. #include<functional>
  17. using namespace std;
  18. typedef long long LL;
  19. typedef unsigned long long ULL;
  20.  
  21. #define MAXN 23
  22. #define INF 0x3f3f3f3f
  23. /*
  24. 给你一个数字M
  25. +-x +-y 能否==M
  26. */
  27. int a[MAXN], n, m;
  28. set<int> s;
  29. int main()
  30. {
  31. int T, tmp;
  32. scanf("%d", &T);
  33. while (T--)
  34. {
  35. s.clear();
  36. scanf("%d", &n);
  37. for (int i = ; i <= n; i++)
  38. scanf("%d", &a[i]);
  39. //int t1, t2;
  40. for (int i = ; i <= n; i++)
  41. {
  42. set<int>::iterator e = s.end();
  43. vector<int> t;
  44. for (set<int>::iterator it = s.begin(); it != s.end(); it++)
  45. {
  46. if (!s.count(*it + a[i]))
  47. t.push_back(*it + a[i]);
  48. if (!s.count(abs(*it - a[i])))
  49. t.push_back(abs(*it - a[i]));
  50. }
  51. s.insert(a[i]);
  52. for (int i = ; i < t.size(); i++)
  53. s.insert(t[i]);
  54. }
  55. scanf("%d", &m);
  56. while (m--)
  57. {
  58. scanf("%d", &tmp);
  59. if (s.count(tmp))
  60. printf("YES\n");
  61. else
  62. printf("NO\n");
  63. }
  64. }
  65. }

Jam's balance set 暴力的更多相关文章

  1. HDU 5616 Jam's balance(DP)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5616 题目: Jam's balance Time Limit: 2000/1000 MS (Java ...

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

    HDU 5616 Jam's balance(Jam的天平) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K ...

  3. HDU 5616 Jam's balance 背包DP

    Jam's balance Problem Description Jim has a balance and N weights. (1≤N≤20)The balance can only tell ...

  4. HDU 5616 Jam's balance(01背包)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5616 题目: Jam's balance Time Limit: 2000/1000 MS (Java ...

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

    Jim has a balance and N weights. (1≤N≤20) The balance can only tell whether things on different side ...

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

    来自官方题解: AC代码: #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream ...

  7. HDU 5616 Jam's balance

    背包.dp[i]=1表示i这种差值能被组合出来,差值有负数,所以用sum表示0,0表示-sum,2*sum表示sum. 询问X的时候,只需看dp[sum+X]或者dp[sum-X]是否有一个为1,注意 ...

  8. hdu 5616 Jam's balance 正反背包+转换

    http://acm.hdu.edu.cn/showproblem.php?pid=5616 思路 题目中蕴含着两种需要计算的重量 1. 从所有的砝码中挑出任意种2.(转换的思想)在天平的两端都挑出这 ...

  9. HDU 5616:Jam's balance(背包DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=5616 题意:有n个物品,每个重量为w[i],有一个天平,你可以把物品放在天平的左边或者右边,接下来m个询问,问是 ...

随机推荐

  1. ASP.NET MVC数据库初始化

    public class DBInitializer:DropCreateDatabaseAlways<BookDBContext> { protected override void S ...

  2. AJPFX关于增强for的概述和使用(foreach)

    增强for的概述和使用(foreach)1.增强for的概述和使用(foreach)                格式:                for(数组或者Collection集合中元素 ...

  3. swiper4初始化/swiper-init/data-swiper

    用data属性初始化swiper <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  4. 重构28-Rename boolean method(重命名布尔方法)

    你也可以说这并不是一个真正的重构,因为方法实际上改变了,但这是一个灰色地带,可以开放讨论.一个拥有大量布尔类型参数的方法将很快变得无法控制,产生难以预期的行为.参数的数量将决定分解的方法的数量.来看看 ...

  5. css定位position属性深究

    1.static:对象遵循常规流.此时4个定位偏移属性不会被应用. 2.relative:对象遵循常规流,并且参照自身在常规流中的位置通过top,right,bottom,left这4个定位偏移属性进 ...

  6. js基础盲点

    var myarray= new Array(8); //创建数组,存储8个数据. 注意:1.创建的新数组是空数组,没有值,如输出,则显示undefined.2.虽然创建数组时,指定了长度,但实际上数 ...

  7. html5开发移动混合App系列2-开发环境搭建(windows)

    Java下载: http://www.oracle.com/technetwork/java/javase/downloads/index.html 环境变量: JAVA_HOME=C:\Progra ...

  8. discuz 插件核心函数hookscript分析.

    function hookscript($script, $hscript, $type = 'funcs', $param = array(), $func = '', $scriptextra = ...

  9. H5 canvas pc 端米字格 写字板

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. tinyXml输出utf-8文档

    TiXmlDocument虽然能读取utf-8的xml文件,但读入后在内存中是以多字节存储.如果新建一个TiXmlDocument,即使定义头为utf-8编码,直接调用SaveFile方法保存的文档仍 ...