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 ... 
随机推荐
- java中的NIO
			使用传统的输入输出流,当读取输入流中的数据如果没有没有读到有效的数据时,程序将在此处阻塞该线程的执行(使用InputStream的read方法从流中读取数据时,如果数据源中没有数据,它也会阻塞该线程) ... 
- 对python变量的理解
			#!/usr/bin/python class Person: '''some words content or descriptions!''' name='luomingchuan' _age = ... 
- React 父组件触发子组件事件
			Parent组件 import React from "react"; import Child from "./component/Child"; class ... 
- 16.unix网络编程一卷 unp.h
			unix网络编程 --ubuntu下建立编译环境 1.安装编译器,安装build-essential sudo apt-get install build-essential 2.下载本书的头文件 下 ... 
- 子线程创建AlertDialog错误
			Can't create handler inside thread that has not called Looper.prepare() 
- ZBrush中自动保存在哪里
			在使用 ZBrush®执行任何会话期间,您都可以设置将文件自动保存,并可以修改保存时间间隔,文件保存位置等设置.发生系统错误后要重新启动ZBrush时,可以从临时文件夹或指定的文件夹中恢复备份文件.如 ... 
- day09-3 数据类型总结,深浅拷贝
			目录 数据类型总结,深浅拷贝 存一个值还是多个值 有序 or 无序 可变 or 不可变 浅拷贝和深拷贝的区别(只针对可变类型) 1.拷贝: 3.深拷贝 总结: 数据类型总结,深浅拷贝 存一个值还是多个 ... 
- 工作需求——VBA操作打印机
			因为最近做的事情比较多,平时也多用EXCEL,所以顺便学习EXCEL的功能性的东西 转载:https://msdn.microsoft.com/zh-tw/vba/excel-vba/articles ... 
- pickle模块 no attribute 'dumps'
			今天写了一个pickle.py的文件练习pickle模块,代码如下: import pickle dic = {"linga": ('football',)} dic2 = {&q ... 
- 用 Java 技术创建 RESTful Web (服务 JAX-RS:一种更为简单、可移植性更好的替代方式)
			作者: Dustin Amrhein, 软件工程师, IBM Nick Gallardo, 软件工程师, IBM 出处: http://www.ibm.com/developerworks/cn/we ... 
