HDU 5161 Jam's balance(暴力打表||动规)

题目链接:点击打开链接

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. Mis 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
一开始用的深搜找答案,果然超时,后来发现其实是可以暴力打表找答案的,还可以神奇的动规写一发!

先记一下我的深搜代码吧

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=1e5+5;
typedef long long LL;
int a[25],n,flag,weight;
void dfs(int step,int ans)
{
    if(ans==weight)
    {
        printf("YES\n");
        flag=1;
        return ;
    }
    if(step==n||flag)
        return ;
    for(int i=0;i<3;i++)
    {
        if(i==0)
        {
            ans+=a[step];
            dfs(step+1,ans);
        }
        if(i==1)
        {
            ans-=a[step];
            dfs(step+1,ans);
        }
        if(i==2)
            dfs(step+1,ans);
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        int m;
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            flag=0;
            scanf("%d",&weight);
            dfs(0,0);
            if(flag==0)
                printf("NO\n");
        }
    }
}
暴力打表

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
bool vis1[2010],vis2[2010];
int a[2010];
int main()
{
    int t,m,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        memset(vis1,false,sizeof(vis1));
        memset(vis2,false,sizeof(vis2));
        vis1[0]=1;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<=2000;j++)
            {
                if(vis1[j])
                {
                    vis2[j]=true;
                    vis2[a[i]+j]=true;
                    vis2[abs(a[i]-j)]=true;
                }
            }
            for(int j=0;j<=2000;j++)
            {
                vis1[j]=vis2[j];
//                vis2[j]=0;
            }
        }
        int m;
        scanf("%d",&m);
        while(m--)
        {
            int p;
            scanf("%d",&p);
            if(vis1[p])
                printf("YES\n");
            else printf("NO\n");
        }
    }
}
神奇的DP

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[22][4110];
int main()
{
    int t,m,n,a[22];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        memset(dp,0,sizeof(dp));
        dp[0][2000]=1;
        for(int i=0;i<n;i++)
        {
            for(int j=a[i];j<=4000-a[i];j++)
            {
                if(dp[i][j])//i代表第几种重量的砝码,j代表该种硬币可以确定的重量
                {
                    //由上一种重量传递到下一种重量,一直这样承接下去,最后dp[n][]是否为真即可
                    dp[i+1][j]=1;
                    dp[i+1][j+a[i]]=1;
                    dp[i+1][j-a[i]]=1;
                }
            }
        }
        scanf("%d",&m);
        while(m--)
        {
            int p;
            scanf("%d",&p);
            if(dp[n][2000-p]||dp[n][2000+p])
                printf("YES\n");
            else printf("NO\n");
        }
    }
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值