HDU 5616 Jam's balance 背包DP
Jam's balance
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.
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.
2
1 4
3
2
4
5
YES
YES
For the Case 1:Put the 4 weight alone
For the Case 2:Put the 4 weight and 1 weight on both side
Jam有NN个砝码和一个没有游标的天平,现在给他(1 \leq N \leq 20)(1≤N≤20)个砝码,砝码可以放左边,也可以放右边,问可不可以测出所问的重量, 问的个数为(1 \leq M \leq 100)(1≤M≤100)个.
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
const int N = ;
int n,m,a[N+],dp[N+],sum;
void DP() {
memset(dp,,sizeof(dp));
dp[]= ;
for(int i = ; i<= n; i++) {
for(int k=;k;k--)
for(int j = sum*;j>=a[i];j--) {
dp[j]|=dp[j-a[i]];
}
}
}
int main() {
int T,x;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);sum=;
for(int i = ;i <= n; i++) scanf("%d", &a[i]),sum+=a[i];
DP();
scanf("%d", &m);
for(int i = ; i<= m; i++) {
scanf("%d", &x);
int g = x+sum&&sum+x>=&&dp[x+sum];
if(g)printf("YES\n");
else printf("NO\n");
}
}
return ;
}
HDU 5616 Jam's balance 背包DP的更多相关文章
- hdu 5616 Jam's balance(dp 正反01背包)
来自官方题解: AC代码: #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream ...
- HDU 5616 Jam's balance(Jam的天平)
HDU 5616 Jam's balance(Jam的天平) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- HDU 5616 Jam's balance(DP)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5616 题目: Jam's balance Time Limit: 2000/1000 MS (Java ...
- HDU 5616 Jam's balance(01背包)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5616 题目: Jam's balance Time Limit: 2000/1000 MS (Java ...
- hdu 5616 Jam's balance 正反背包+转换
http://acm.hdu.edu.cn/showproblem.php?pid=5616 思路 题目中蕴含着两种需要计算的重量 1. 从所有的砝码中挑出任意种2.(转换的思想)在天平的两端都挑出这 ...
- HDU 5616 Jam's balance
背包.dp[i]=1表示i这种差值能被组合出来,差值有负数,所以用sum表示0,0表示-sum,2*sum表示sum. 询问X的时候,只需看dp[sum+X]或者dp[sum-X]是否有一个为1,注意 ...
- HDU 5501 The Highest Mark 背包dp
The Highest Mark Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- HDU 1011 Starship Troopers 树形+背包dp
http://acm.hdu.edu.cn/showproblem.php?pid=1011 题意:每个节点有两个值bug和brain,当清扫该节点的所有bug时就得到brain值,只有当父节点被 ...
- HDU 5119 Happy Matt Friends (背包DP + 滚动数组)
题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt ...
随机推荐
- XCODE插件 之 Code Pilot 无鼠标化
什么是Code Pilot? Code Pilot 是一个 Xcode 5 插件.同意你不许使用鼠标就能高速地查找项目内的文件.方法和标识符. 它使用模糊查询匹配(fuzzy query matchi ...
- CXF WebService中传递复杂对象(List、Map、Array)
转自:https://wenku.baidu.com/view/047ce58ed0d233d4b14e69eb.html 现在开始介绍传递复杂类型的对象.如JavaBean.Array.List.M ...
- MySQL 我自己常用的语句汇总
1,更新,根据一个表更新另一个表,比如批量同步外键 方法一: update 更新表 set 字段 = (select 参考数据 from 参考表 where 参考表.id = 更新表.id); up ...
- tp5数据库操作 Db类
一.链接数据库 1.配置文件定义 application\database.php 注意:数据表前缀更改,在文件的prefix选项 2.类定义 二.数据库的基本使用 namespace app\de ...
- 使用最新vue_cli搭建的模版
使用最新vue_cli搭建的模版,包含了常用的插件,router和axiox与测试插件.项目的结构如下: 使用之前请打开 REAMME.md 看看. 已经搭建好的框架的下载地址:https://sha ...
- 云上建站快速入门:博客、论坛、CMS、电子商务网站统统搞定
现在制作一个网站已经越来越容易了,只要知道清晰的流程之后都是可以很快的建好一个企业或者个人网站的!免费的建站程序很多,下面听哥给你亮出来,建站一般来说分主要有这四步:申请域名.申请虚拟主机.制作网页, ...
- Pyhton学习——Day41
#一个完整的 JavaScript 实现是由以下 3 个不同部分组成的:# 核心(ECMAScript)# 文档对象模型(DOM) Document object model (整合js,css,ht ...
- pythone 学习笔记(粗略)
文档目录 概述 安装 基本语法 数据结构 4.1 数字和字符串类型 4.2 元祖 4.3 列表 4.4 字典 流程语句 5.1 分支结构 5.2 逻辑运算符(if) 5.3 循环 5.3.1 for ...
- HDU 5763 Another Meaning (KMP/哈希+DP)
题目大意:给你两个串,一长一短,如果长串中某个子串和短串完全相同,则这个子串可以被替换成"#",求长串所有的表达形式....... 比如"hehehehe"和& ...
- WordCount合作--自己部分
前言: (1)合作者:201631062127,201631062625 (2)合作代码地址:WordCount 一.结对的PSP表格: PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟 ...