BestCoder Round #70

B:

Jam's balance




Jam's balance

Accepts: 143
Submissions: 880
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Problem Description

Jim has a balance and N weights. (1≤N≤20)(1 \leq N \leq 20)(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(1≤T≤5)T(1 \leq T \leq 5)T(1T5), means T test cases.For each test case :The first line is NNN, means the number of weights.The second line are NNN number, i'th number wi(1≤wi≤100)w_i (1 \leq w_i \leq 100)wi(1wi100) means the i'th weight's weight is wiw_iwi.The third line is a number MMM. MMM 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


之前在省赛中做过类似的题目 不过上次是暴力 这次需要DP来优化

CODE:

 #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <algorithm>
    using namespace std;
    int fn;
    int hash[105][105],dp[30][2005],x,y,MAX,n[30],sum;
    int wtf[2005];
    int cmp(int x,int y)
    {
        return x>y;
    }

    void solve()
    {
        int i,j,k,l,w;
        memset(dp,0,sizeof(dp));
        dp[0][0] = 1;
        int ans = 0;
        for(i = 1; i<=fn; i++)
        {
            w = n[i];
            memcpy(dp[i],dp[i-1],sizeof(dp[0]));
            for(j = sum; j>0; j--)
            {
                if(j>=w)//可以放置,差j-w
                {
                    if(dp[i-1][j-w])//补成j
                        dp[i][j] = 1;
                    if(dp[i-1][j])//差j-w
                        dp[i][j-w] = 1;
                }
                else
                {
                    if(dp[i-1][j])//差w-j
                    {
                        dp[i-1][w-j] = 1;
                        dp[i][w-j]=1;
                    }
                }
            }
        }
        for(i = 1; i<=sum; i++)
        {
            if(dp[ fn ][i])
            {
                wtf[ i]=1;
                ans++;
            }
        }
        if(ans>MAX)
            MAX = ans;
    }

    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            memset(wtf,0,sizeof(wtf));
            int i,j;
            sum=0;
            scanf("%d",&fn);
            for(i=1;i<=fn;i++)
            {
            scanf("%d",&n[i]);
            sum+=n[i];
            }

            MAX = 0;


            solve();


            int m;
            scanf("%d",&m);
            while(m--)
            {
                int fff;
                scanf("%d",&fff);
                if(fff>2000)
                {
                     printf("NO\n");
                     continue;
                }

                if(wtf[fff])
                    printf("YES\n");
                else
                    printf("NO\n");
            }


        }

        return 0;
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值