zoukankan      html  css  js  c++  java
  • U

    U - The Balance

    Now you are asked to measure a dose of medicine with a balance and a number of weights. Certainly it is not always achievable. So you should find out the qualities which cannot be measured from the range [1,S]. S is the total quality of all the weights.

    Input

    The input consists of multiple test cases, and each case begins with a single positive integer N (1<=N<=100) on a line by itself indicating the number of weights you have. Followed by N integers Ai (1<=i<=N), indicating the quality of each weight where 1<=Ai<=100.

    Output

    For each input set, you should first print a line specifying the number of qualities which cannot be measured. Then print another line which consists all the irrealizable qualities if the number is not zero.

    Sample Input

    3
    1 2 4
    3
    9 2 1
    

    Sample Output

    0
    2
    4 5
    

    题意:

    给你n个砝码,sum是n个砝码的重量之和,在此区间[1,sum]有多少个重量不能用天平称出来,输出个数,并输出这些不能称出来的质量

    分析:

    dp[i][j]表示只用前i个砝码称出 j 这个质量的方案数目

    因为第i个砝码可以选择不放,也可以选择放一侧,或者对立的一侧

    所以dp[i][j]=dp[i1][j]+dp[i1][jval[i]]+dp[i1][j+val[i]]+dp[i1][val[i]j]dp[i][j]=dp[i-1][j]+dp[i-1][j-val[i]]+dp[i-1][j+val[i]]+dp[i-1][val[i]-j]

    #include<cstdio>
    #include<algorithm>
    #include<string>
    #include<cstring>
    #include<iostream>
    #include<iomanip>
    #include<stack>
    #include<vector>
    #define mset(a,b)   memset(a,b,sizeof(a))
    using namespace std;
    typedef unsigned long long ull;
    typedef long long ll;
    const int maxn=2e4+10;
    const int branch=26;
    const int inf=0x3f3f3f3f;
    const int MOD=1e6+7;
    int dp[130][maxn],vis[130];
    int n,maxx;
    int mmp[maxn],top;
    void solve()
    {
        mset(dp,0);
        dp[0][0]=1;
        for(int i=1;i<=n;++i)//可以使用第i中硬币下
        {
            for(int j=0;j<=maxx;++j)
            {
                dp[i][j]=dp[i-1][j];
                if(j>=vis[i])//加上去
                    dp[i][j]+=dp[i-1][j-vis[i]];
                dp[i][j]+=dp[i-1][j+vis[i]];//把这个硬币放在对面
                if(vis[i]-j>=0)//把这个硬币放在对面
                    dp[i][j]+=dp[i-1][vis[i]-j];
            }
        }
        top=0;
        for(int i=1;i<=maxx;++i)
            if(!dp[n][i])
               mmp[top++]=i;
        printf("%d
    ",top);
        for(int i=0;i<top;++i)
            printf("%d%c",mmp[i],(i==top-1)?'
    ':' ');
    }
    int main()
    {
        while(~scanf("%d",&n))
        {
            maxx=0;
            for(int i=1;i<=n;++i)
            {
                scanf("%d",vis+i);
                maxx+=vis[i];
            }
            solve();
        }
        return 0;
    }
    
  • 相关阅读:
    修改项目名称
    Servlet中request、response、ServletContext 及其常用方法
    Servlet生命周期、常用提交方式、中文乱码问题、重定向和请求转发
    JavaScript基础(三) 事件
    JavaScript基础(二) DOM
    JavaScript基础(一)
    css基础:样式之定位、样式之隐藏、二级菜单、多个列表转表格、图片精灵技术
    css基础:表格样式、一级菜单、BFC浮动影响
    css基础及其实例:常用选择器、颜色五中写法、字体样式、自定义字体、盒模型及其样式设置
    HTML元素标签及表单元素详解
  • 原文地址:https://www.cnblogs.com/dchnzlh/p/10427267.html
Copyright © 2011-2022 走看看