题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5616

题目:

Jam's balance

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1810    Accepted Submission(s): 754

Problem Description
 
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
 
题意:
给若干个砝码和一个天平,再给若干个要称的重量,问是否能由以上的玛法和天平秤出。
 
思路:
因为砝码可以放在天平两侧,所以砝码重量之和以及重量之差 都能秤出来。所以状态转移分两个:
1.该重量大于等于当前遍历的砝码重量时:dp[j]=max(dp[j], dp[j-weight[i]]);
2.该重量小于当前遍历的砝码重量时:dp[j]=max(dp[j], dp[weight[i]-j]);
注意点是,要先将砝码的重量进行升序排序。以免漏掉第二种情况。
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main(){
int t,n,m;
int weight[];
int dp[];
int sum;
scanf("%d",&t);
while (t--) {
scanf("%d",&n);
memset(dp, , sizeof(dp));
dp[]=;
sum=;
for (int i=; i<n; i++) {
scanf("%d",&weight[i]);
sum+=weight[i];
}
sort(weight, weight+n);
for (int i=; i<n; i++) {
for (int j=sum; j>=; j--) {
if(j>=weight[i])dp[j]=max(dp[j], dp[j-weight[i]]);
else dp[j]=max(dp[j], dp[weight[i]-j]);
}
}
scanf("%d",&m);
for (int i=; i<m; i++) {
int w;
scanf("%d",&w);
printf("%s\n",dp[w]?"YES":"NO");
}
}
return ;
}

HDU 5616 Jam's balance(DP)的更多相关文章

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

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

  2. 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 ...

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

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

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

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

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

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

  6. HDU 5616 Jam's balance

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

  7. HDU 5617 Jam's maze dp+滚动数组

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5617 bc(中文):http://bestcoder.hdu.edu.cn/contest ...

  8. cdq分治(hdu 5618 Jam's problem again[陌上花开]、CQOI 2011 动态逆序对、hdu 4742 Pinball Game、hdu 4456 Crowd、[HEOI2016/TJOI2016]序列、[NOI2007]货币兑换 )

    hdu 5618 Jam's problem again #include <bits/stdc++.h> #define MAXN 100010 using namespace std; ...

  9. hdu 5616

    Jam's balance Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

随机推荐

  1. 汽车Vin码识别技术的由来到发展

    核心内容:汽车Vin码.汽车车架号.Vin码识别.汽车Vin码识别.车架号识别.汽车车架号识别 一.汽车Vin码识别应用背景 随着二手车产业链发展越来越强大,在汽车买卖以及后市场应用中,了解车辆的相关 ...

  2. jQuery UI 入门之实用实例

    jQuery UI 入门 jQuery UI 简介 jQuery UI 是一个建立在 jQuery JavaScript 库上的小部件和交互库,您可以使用它创建高度交互的 Web 应用程序.无论您是创 ...

  3. java 1.8 动态代理源码分析

    JDK8动态代理源码分析 动态代理的基本使用就不详细介绍了: 例子: class proxyed implements pro{ @Override public void text() { Syst ...

  4. dedecms后台添加新变量和删除变量的方法

    下面由做网站为大家来介绍dedecms后台添加新变量和删除变量的方法 添加新变量是做什么用的?答:可以在模板内调用的东东. 一.进入网站织梦(Dedecms)后台(以dede5.5为例),依次打开系统 ...

  5. Mac OS中使用VScode配置C语言开发环境

    个人博客 chinazt.cc 闲话少叙,直奔主题 下载VSCode https://code.visualstudio.com/download 安装C/C++插件 需要两个插件: 1. cppto ...

  6. 网页中嵌入百度地图报错:The request has been blocked,the content must served over Https

    网页中嵌入百度地图 1.进入百度地图开发平台:http://lbsyun.baidu.com/index.php?title=jspopular 2.获取密钥:http://lbsyun.baidu. ...

  7. nginx之 nginx-1.9.7 + tomcat-8.5.15 反向代理+应用负载均衡 安装配置

    环境说明:nginx 反向代理服务器 ip 为: 10.219.24.26tomcat1 应用服务器 ip 为: 10.219.24.21tomcat3 应用服务器 ip 为: 10.219.24.2 ...

  8. Compare and Swap [CAS] 算法

    一个Java 5中最好的补充是对原子操作的支持类,如AtomicInteger,AtomicLong等.这些类帮助你减少复杂的(不必要的)多线程代码,实际上只是完成一些基本操作,如增加或减少多个线程之 ...

  9. 【Python3之基本数据类型,基本运算】

    一.基本数据类型 1.字符串 类:str 方法:选中str,按住command(ctrl)+左键跳转至对应的方法 创建 a = "hexin" a = str('hexin') 转 ...

  10. 自动适配H5容器(UIViewView/WKWebView),生成长图,防微信进度条

    前段时间撸代码猥琐发育的时候,设计师老王给了张截图某宝APP上一个生成长图分享的功能,正好公司有这个需求,于是在立马开始操练起来!在万能的度娘上搜集整理资料后发现很多文章介绍的方案对WKWebView ...