Jim has a balance and N weights. (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.
Input
The first line is a integer T(1≤T≤5), means T test cases.
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.
Output
You 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

题意:
给你n个砝码,q个询问,对于每一个询问,你需要回答这n个玛法能否选取一些玛法的组合称量出x的重量。砝码可以放在天平的左侧和右侧。
思路:

01背包基础题。

定义dp[x] =1 表示可以测量x重量。

正着扫一遍,反着扫一遍即可,代码有注释。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int dp[maxn];
int a[maxn];
int main()
{//freopen("D:\\code\\text\\input.txt","r",stdin);//freopen("D:\\code\\text\\output.txt","w",stdout);int t;gbtb;cin>>t;while(t--){int n,q,w;cin>>n;int sum=0;repd(i,1,n){cin>>a[i];sum+=a[i];}MS0(dp);dp[0]=1;repd(i,1,n){for(int j=sum;j>=a[i];--j)//   一定要反着dp,,因为砝码的个数1 个,{// 从后向前可以避免当前修改的dp[j]在本次过程中的后续中有影响。if(dp[j-a[i]]==1){dp[j]=1;}}}repd(i,1,n){for(int j=1;j<=sum;++j){if(dp[j+a[i]]==1)// 把a[i]放在天平的反侧,就可以测量出 j的重量。{dp[j]=1;}}}cin>>q;while(q--){cin>>w;if(dp[w]){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}}}return 0;
}inline void getInt(int* p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}}else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}}
}

转载于:https://www.cnblogs.com/qieqiemin/p/11216002.html

Jam's balance HDU - 5616 (01背包基础题)相关推荐

  1. hdu 2546 饭卡【贪心+01背包基础题】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=2546 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  2. hdu 2955 01背包

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 如果认为:1-P是背包的容量,n是物品的个数,sum是所有物品的总价值,条件就是装入背包的物品的体积和不能 ...

  3. hdu 3732(01背包转多重背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3732 思路:这么大的数据,用01背包肯定会TLE的,01背包转多重背包..最多也就11*11=121件 ...

  4. HDU 2546 饭卡(01背包裸题)

    饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  5. POJ 3624 Charm Bracelet(01背包 基础)

    题意: n个装饰品 容量m的背包 每个装饰品 重wi 价值 di 求能装的最大价值 思路:基础01背包 #include<stdio.h> #include<string.h> ...

  6. HDU 3466 01背包变形

    给出物品数量N和总钱数M 对于N个物品.每一个物品有其花费p[i], 特殊值q[i],价值v[i] q[i] 表示当手中剩余的钱数大于q[i]时,才干够买这个物品 首先对N个物品进行 q-p的排序,表 ...

  7. hdu 1574(01背包)

    RP问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Des ...

  8. HDU 2546(01背包)

    饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  9. hdu 2184 01背包变形

    转自:http://blog.csdn.net/liuqiyao_01/article/details/8753686 题意:这是又是一道01背包的变体,题目要求选出一些牛,使smartness和fu ...

最新文章

  1. 函数初识(文字总结)
  2. 密码学入门1——凯撒密码和三重DES加解密
  3. SQL Server 涉及数据库安全常用SQL语句
  4. 论文学习2-Incorporating Graph Attention Mechanism into Knowledge Graph Reasoning Based on Deep Reinforce
  5. Qt工作笔记-Qt移植到Linux上时提升窗口部件大小写问题(VS上存在的问题)
  6. 关于Web面试的基础知识点--Javascript(一)
  7. 判定两个点是否在一条直线的同一侧_计算几何01-判定两条线段是否相交
  8. python读取数据集前十行_Python读取数据集并消除数据中的空行方法
  9. 【BZOJ3992】【SDOI2015】序列统计
  10. Halcon学习路线——Blob分析(2)
  11. [Web 前端] 005 html 常用标签补充
  12. 软件测试中单元测试的内容有哪些?-alltesting云测试
  13. 《快速掌握QML》第六章 动画
  14. sonarqube+scanner代码质量检查
  15. MTK平台如何决定SensorMode
  16. 【CXY】JAVA基础 之 语法基础
  17. 12个球和一个天平,现知道只有一个和其它的重量不同,问怎样称才能用三次就找到那个球,13个呢
  18. Npm依赖检查版本及升级
  19. Codeforces Round #700 (Div. 2) C. Searching Local Minimum(交互)
  20. 二项分布均值和方差的推导

热门文章

  1. one order event trace - how to switch on
  2. 运行npm update等命令出错后如何分析问题根源
  3. 如何计算CDS view里两个时间戳之间的天数间隔
  4. 没有收到回复的同学注意了,用它一键查询!
  5. flutter 获取定位_从头开发一个Flutter插件(二)高德地图定位插件
  6. python比较日期大小_Python日期的处理——datetime模块
  7. 前端接收pdf文件_雷达接收机的噪声系统及灵敏度
  8. python 列表解析式_python列表解析式,字典解析式,集合解析式和生成器
  9. php ping主机名,PHP PING值函数
  10. php+header+跳转输出,php利用header跳转怎么失效了?