hdu 5616

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. (1N20)
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(1T5) , 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(1wi100) 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
");
                else
                    printf("NO
");
            }
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/hsd-/p/5174028.html