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

题意:

Jam有NN个砝码和一个没有游标的天平,现在给他(1 \leq N \leq 20)(1≤N≤20)个砝码,砝码可以放左边,也可以放右边,问可不可以测出所问的重量, 问的个数为(1 \leq M \leq 100)(1≤M≤100)个.

题解:

这道题可以放左边,可以放右边,N=20N=20显然每种状态都枚举是不太现实的,因为每组砝码都可以变成很多种重量,当然也不排除有人乱搞过了这一题,其实这道题是一道贪心的思想,我们看到ww不大,所以可以用0101背包扫一次,当然这还是不够的,这只能放一边,考虑到可以放另一边,就是可以有减的关系,所以反着再背包一遍,注意要判断边界。

#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
const int  N = 5000;
int n,m,a[N+10],dp[N+10],sum;
void DP() {
    memset(dp,0,sizeof(dp));
    dp[0]= 1;
    for(int i = 1; i<= n; i++) {
        for(int k=2;k;k--)
        for(int j = sum*2;j>=a[i];j--) {
            dp[j]|=dp[j-a[i]];
        }
    }
}
int main() {
   int T,x;
   scanf("%d",&T);
   while(T--) {
    scanf("%d",&n);sum=0;
    for(int i = 1;i <= n; i++) scanf("%d", &a[i]),sum+=a[i];
    DP();
    scanf("%d", &m);
    for(int i = 1; i<= m; i++) {
        scanf("%d", &x);
        int g = x+sum&&sum+x>=0&&dp[x+sum];
        if(g)printf("YES\n");
        else printf("NO\n");
    }
   }
    return 0;
}

HDU 5616 Jam's balance 背包DP

时间: 2024-10-09 14:55:20

HDU 5616 Jam's balance 背包DP的相关文章

hdu 5616 Jam&#39;s balance(dp 正反01背包)

来自官方题解: AC代码: 1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<math.h> 7 #include<algorithm> 8 #include<queu

HDU 5616 Jam&#39;s balance(DP)

题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5616 题目: Jam's balance Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1810    Accepted Submission(s): 754 Problem Description Jim has a balance an

HDU 5616 Jam&#39;s&#160;balance(Jam的天平)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: center; font-family: 宋体; color: rgb(26,92,200); font-weight: bold; fo

HDU 5616 Jam&#39;s balance(暴力枚举子集)

题目链接:点击打开链接 题意:有一个没有游标的天平,和n个秤砣,m个询问, 每次一个k,问可否秤出k这个重量. 秤砣可以放两边. 思路:因为n最大20, 暴力枚举子集. 因为可以放两边, 所以每次再跑一遍, 减去每个的重量, 将答案保存. 比赛的时候忘了限制边界,虽然过了终测数据, 却被人用大数据hack了(RE), 还是自己程序写的不够鲁棒, 思考的不完善. 细节参见代码: #include<cstdio> #include<cstring> #include<algori

HDU 5617 Jam&#39;s maze(DP)

题目链接:点击打开链接 题意:给你一个n*n的矩阵.  求从(1,1)走到(n,n)所组成的回文串个数. 思路:一开始傻逼把状态写成了d[x][y][s],s表示一个串, 用map存的, 后来发现极不可行, 因为这个状态简直太大了, 包括了s串的所有情况. 只是相当于一个dfs中的剪枝罢了. 后来想到, 其实串是不必记录的, 我们只要统计个数, 所以不妨在DP的过程中就判断回文串的情况, 那么就需要同时记录两头的情况.  为了不爆内存, 将状态表示成d[i][x1][x2], 表示走了i步, 左

HDU 5617 Jam&#39;s maze 巧妙DP

题意:给你一个字符矩阵,从(1,1)到(n,n)有很多种走法,每一种走法形成一个字符串,问有多少种走法形成的字符串是回文的 分析:(粘贴BC题解) 的是回文串,有人会想到后缀数组自动机马拉车什么的,其实只要求方案数很多,所以我们应该想到动态规划,首先是状态的定义,我们可以想着从(1,1)(1,1)和(n,n)(n,n)开始走,然后走到相遇.所以我们定义状态可以定义为f[x_1][y_1][x_2][y_2]f[x?1??][y?1??][x?2??][y?2??]表示所对应的两个点(x_1,y_

HDU 5119 Happy Matt Friends (背包DP + 滚动数组)

题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt's friends has a magic number. In the game, Matt selects some (could be zero) of his friends. If the xor (exclusive-or) sum of the selected friends'ma

HDU 2602 Bone Collector (01背包DP)

题意:给定一个体积,和一些物品的价值和体积,问你最大的价值. 析:最基础的01背包,dp[i] 表示体积 i 时最大价值. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream>

POJ 1837 Balance 背包dp

点击打开链接 Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11067   Accepted: 6865 Description Gigel has a strange "balance" and he wants to poise it. Actually, the device is different from any other ordinary balance. It orders t