Jam's balance

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

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

Hint

For the Case 1:Put the 4 weight alone
For the Case 2:Put the 4 weight and 1 weight on both side

 
Source
补一道bc b题  一会马上cf
题意  t组数据 n个砝码 m个询问 问当前询问能否 使用已知砝码秤出
题解
比赛的时候 dfs暴搜 3^20 超时了
后来看别人题解代码
看到这样一个 很好理解的  码了一个 算是dp吧
通过 dp2数组 在增加j砝码的情况下(放左 放右 不放) 不断更新dp1数组  (数组序号代表 --目标秤重)
#include<bits/stdc++.h>
using namespace std;
int t;
int n;
int fa[25];
map<int,int>dp1;
map<int,int>dp2;
int sum;
int m,exm;
int main()
{
while(scanf("%d",&t)!=EOF)
{
for(int i=1; i<=t; i++)
{
sum=0;
dp1.clear();
dp2.clear();
scanf("%d",&n);
for(int j=0; j<n; j++)
{
scanf("%d",&fa[j]);
sum+=fa[j];
}
dp1[0]=1;
dp1[fa[0]]=1;
for(int j=1; j<n; j++)
{
dp2.clear();
for(int k=0; k<=sum; k++)
{
if(dp1[k])
{
dp2[k]=1;
dp2[k+fa[j]]=1;
dp2[abs(k-fa[j])]=1;
}
}
for(int k=0; k<=sum; k++)
{
if(dp2[k]&&!dp1[k])
dp1[k]=1;
}
}
scanf("%d",&m);
for(int j=1; j<=m; j++)
{
scanf("%d",&exm);
if(dp1[exm])
printf("YES\n");
else
printf("NO\n");
}
}
}
return 0;
}

  

hdu 5616的更多相关文章

  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)

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

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

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

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

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

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

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

  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 背包DP

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

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

随机推荐

  1. 前端开发工程师 - 01.页面制作 - 第3章.HTML

    第3章--HTML HTML简介 Hyper Text Markup Language:超文本标记语言--用于标记网页的内容 history: html(1991)雏形 -> html4.01( ...

  2. 现实世界中的 Python

    Python 有多稳定? 非常稳定. 自 1991 年起大约每隔 6 到 18 个月就会推出新的稳定发布版,这种状态看来还将持续下去. 目前主要发布版本的间隔通常为 18 个月左右. 开发者也会推出旧 ...

  3. TW实习日记:第13天

    昨天困扰的问题终于解决了.因为是百度地图api提供的函数,所以这个解决办法并不适用于所有异步请求,仅仅针对百度地图api的调用接口函数和回调函数.有两种解决方法可以解决百度地图api中常出现的请求回调 ...

  4. [Clr via C#读书笔记]Cp15枚举和位标识

    Cp15枚举和位标识 枚举类型 本质是结构,符号名称-值:好处显而易见:System.Enum;值类型: 编译的时候,符号会转换为常量字段: 枚举支持很多方法和成员: 位标识bit flag 判断和设 ...

  5. [Clr via C#读书笔记]Cp10属性

    Cp10属性 属性的本质就是方法,只是看起来像字段罢了: 无参属性 就是一般属性: 字段一般要private,然后通过设置访问方法-访问器来访问:属性是方法语法变种:getset不一定要访问支持字段: ...

  6. tabales1.10版参数详解

    //@translator codepiano //@blog codepiano //@email codepiano.li@gmail.com //尝试着翻译了一下,难免有错误的地方,欢迎发邮件告 ...

  7. javaIO--文件操作类

    文件操作类主要是使用File类的各种方法对文件和目录进行操作.包括文件名.文件长度.最后修改时间和是否只读等,提供获得当前文件的路径名.判断文件是否存在.创建.删除文件和目录等一系列的操作方法. 下面 ...

  8. Debian常用設置

    1. 更新軟件源 sudo cp /etc/apt/sources.list /etc/apt/sources.list_bak #備份 sudo vi /etc/apt/sources.list / ...

  9. 【MVC4升级到MVC5】ASP.Net MVC 4项目升级MVC 5的方法

    1.备份你的项目 2.从Web API升级到Web API 2,修改global.asax,将 ? 1 WebApiConfig.Register(GlobalConfiguration.Config ...

  10. 什么是RESTFUL协议?

    1,restful是Representational State Transfer的缩写,翻译过来是表现层状态转移.我的理解是去掉访问文件的格式,比如去掉文件为html的.html,而是采用路径的方式 ...