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. python中出现ascii编码问题的解决办法

    一劳永逸,一次性全盘解决的办法 环境变量中去设置 以centos 7为例: vim /etc/profile export PYTHONIOENCODING=utf-8 source /etc/pro ...

  2. 下拉网页div自动浮在顶部

    <!DOCTYPE html> <html> <head> <title></title> <style type="tex ...

  3. Redis4.0支持的新功能说明

    本文以华为云DCS for Redis版本为例,介绍Redis4.0的新功能.文章转载自华为云帮助中心. 与Redis3.x版本相比,DCS的Redis4.x以上版本,除了开源Redis增加的特性之外 ...

  4. 蓝桥杯算法训练 区间k大数查询

    算法训练 区间k大数查询   问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示序列长度. 第二行包含n个正整数,表示给定的序列. 第三个 ...

  5. 提升方法-AdaBoost

    提升方法通过改变训练样本的权重,学习多个分类器(弱分类器/基分类器)并将这些分类器进行线性组合,提高分类的性能. AdaBoost算法的特点是不改变所给的训练数据,而不断改变训练数据权值的分布,使得训 ...

  6. openstack如何整合vmare最佳方案

    OpenStack中国社区编者按:通过多年的发展,VMWare在虚拟化市场处于领军地位,很多企业部署了VMWare虚拟化方案,随着OpenStack云计算平台的快速崛起,很多企业都面临一个问题:能否. ...

  7. 自测之Lesson6:文件I/O

    题目:区分文件I/O和标准I/O. 区别: ①首先两者一个显著的不同点在于,标准I/O默认采用了缓冲机制,比如调用fopen函数,不仅打开一个文件,而且建立了一个缓冲区(读写模式下将建立两个缓冲区), ...

  8. “Hello World”团队第一周博客汇总

    时间:2017-10-13——2017-10-19 Scrum会议: 会议要求博客:https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/1 ...

  9. .net平台借助第三方推送服务在推送Android,IOS消息(极光推送_V3版本)最新

    最近刚从极光推送官网上看到V2版本要停用,不得不有重新写V3版本的.这里用到了 HTTP Basic Authentication http://www.cnblogs.com/pingming/p/ ...

  10. Visual Studio 数据库架构比较

      一.前言 开发的时候在测试服务器上和线网服务器上面都有我们的数据库,当我们在线网上面修改或者新增一些字段后,线网的数据库也需要更新,这个时候根据表的修改记录,然后在线网上面一个一个增加修改很浪费效 ...