• 集训第五周 动态规划 K题 背包


    K - 背包
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
    Submit Status

    Description

    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
     
     
    这道题可以使用DP做,dp(i)代表i这个数字是否可称,dp(i)=(( dp(j)==1 && 1<j<sum )     j==i + || - 新加入的数字 ?)
     
    #include"iostream"
    #include"algorithm"
    #include"cstring"
    using namespace std;
    
    const int maxn=10010;
    
    int dp[maxn],ok[maxn],ans[maxn];
    
    int main()
    {
        int sum,n,temp;
    
        while(cin>>n)
        {
            memset(dp,0,sizeof(dp));
            dp[0]=1;
            sum=0;
            for(int i=1;i<=n;i++)
            {
                cin>>temp;
                memset(ok,0,sizeof(ok));
                sum+=temp;
                for(int j=0;j<=sum;j++) if(dp[j])
                {
                    ok[j+temp]=1;
                    ok[abs(j-temp)]=1;
                }
                for(int j=1;j<=sum;j++) if(ok[j])
                dp[j]=1;
            }
            int top=0;
            for(int i=1;i<=sum;i++)
            {
                if(dp[i]==0) ans[top++]=i;
            }
            cout<<top<<endl;
            if(top)
            {
            for(int j=0;j<top-1;j++) cout<<ans[j]<<" ";
            cout<<ans[top-1]<<endl;
            }
        }
        return 0;
    }
    你好
  • 相关阅读:
    Schema和数据类型优化?
    语雀发布博客园
    为知笔记文章目录
    码云搭建博客
    springboot的过滤器、监听器、拦截器
    springboot常用注解
    springboot使用小技巧合集
    springboot整合swagger2
    强制卸载win软件
    xshell下载和优化配置
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/4728532.html
Copyright © 2020-2023  润新知