zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    hdoj 2803 The MAX【简单规律题】
    hdoj 2579 Dating with girls(2)【三重数组标记去重】
    hdoj 1495 非常可乐【bfs隐式图】
    poj 1149 PIGS【最大流经典建图】
    poj 3281 Dining【拆点网络流】
    hdoj 3572 Task Schedule【建立超级源点超级汇点】
    hdoj 1532 Drainage Ditches【最大流模板题】
    poj 1459 Power Network【建立超级源点,超级汇点】
    hdoj 3861 The King’s Problem【强连通缩点建图&&最小路径覆盖】
    hdoj 1012 u Calculate e
  • 原文地址:https://www.cnblogs.com/hsd-/p/5174028.html
Copyright © 2011-2022 走看看