• 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;
    }
    

      

  • 相关阅读:
    详解事件驱动跟消息驱动机制相比
    浏览器加载 CommonJS 模块的原理与实现
    全面理解 javascript 的 argements caller callee call apply 之caller
    在javaScript中检测数据类型的几种方式
    Netbeans IDE 安装Emmet插件并解决Emmet插件无效果问题
    JS的 try catch使用心得
    safari 浏览器 input textarea select 等不能响应用户输入
    js十大排序算法
    js实现 页面加载 完成 后顺序 执行
    原生JS封装 toast 弹层,自动关闭
  • 原文地址:https://www.cnblogs.com/hsd-/p/5174028.html
Copyright © 2020-2023  润新知