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. 【转】MMO即时战斗:地图角色同步管理和防作弊实现

    ---转自CSDN 一.前言 无论是端游.页游.手游如果是采用了MMO即时战斗游戏模式,基本都会遇到同屏多角色实时移动.释放技能.战斗等场景,于是自然也需要实现如何管理同屏内各种角色的信息同步:例如角 ...

  2. Intro to Probabilistic Model

    概率论复习 概率(Probability) 频率学派(Frequentist):由大量试验得到的期望频率(致命缺陷:有些事情无法大量试验,例如一封邮件是垃圾邮件的概率,雷达探测的物体是一枚导弹的概率) ...

  3. .net转PHP从零开始-环境的搭建

    PHP初级开发环境安装很简单,只需要使用一键安装的phpstudy 下载地址:http://www.phpstudy.net/ 安装后可以看到 这样的界面,设置好相关的配置,然后,选择查看phpinf ...

  4. 第八章 IO库

    8.1&&8.2 #include <iostream> #include <vector> #include <string> using nam ...

  5. 团队协作第八周个人PSP

    11.3 --11.9本周例行报告 1.PSP(personal software process )个人软件过程. 类型 任务 开始时间                结束时间 中断时间 实际用时 ...

  6. 计算器软件实现系列(五)策略模式+asp.net

    一 策略模式代码的编写 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// ...

  7. 福大软工1816:Alpha(4/10)

    Alpha 冲刺 (4/10) 队名:Jarvis For Chat 组长博客链接 本次作业链接 团队部分 工作情况汇报 张扬(组长) 过去两天完成了哪些任务: 文字/口头描述: 1.将中文分词.词频 ...

  8. Java中的线程同步

    Java 中的线程同步问题: 1. 线程同步: 对于访问同一份资源的多个线程之间, 来进行协调的这个东西. 2. 同步方法: 当某个对象调用了同步方法时, 该对象上的其它同步方法必须等待该同步方法执行 ...

  9. TCP系列01—概述及协议头格式

    一.TCP简单介绍 我们经常听人说TCP是一个面向连接的(connection-oriented).可靠的(reliable).字节流式(byte stream)传输协议,  TCP的这三个特性该怎么 ...

  10. c语言作业1