• HDU 2844 Coins(二进制优化)


      -

    Coins

    题目链接

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 16702 Accepted Submission(s): 6634

    Problem Description
    Whuacmers use coins.They have coins of value A1,A2,A3…An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn’t know the exact price of the watch.

    You are to write a program which reads n,m,A1,A2,A3…An and C1,C2,C3…Cn corresponding to the number of Tony’s coins of value A1,A2,A3…An then calculate how many prices(form 1 to m) Tony can pay use these coins.

    Input
    The input contains several test cases. The first line of each test case contains two integers n(1 ≤ n ≤ 100),m(m ≤ 100000).The second line contains 2n integers, denoting A1,A2,A3…An,C1,C2,C3…Cn (1 ≤ Ai ≤ 100000,1 ≤ Ci ≤ 1000). The last test case is followed by two zeros.

    Output
    For each test case output the answer on a single line.

    Sample Input
    3 10
    1 2 4 2 1 1
    2 5
    1 4 2 1
    0 0
    
    
    Sample Output
    8
    4
    
    
    Source
    2009 Multi-University Training Contest 3 - Host by WHU
    
    ///题意:有一些被划分为1-6价值的石头,并一直每个价值有多少块,求可否将石头分成两份且价值相等。
    ///思路:求出总价值,除2。转化为大小为(总价值/2)的背包可否恰好装满的问题。
    #include<algorithm>
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int dp[100050];
    const int inf=0x3f3f3f3f;
    int main()
    {
        int a[15],b[15],st[1040];
        int t=1;
        while(1)
        {
            int sum=0;
            for(int i=0;i<6;i++)
                scanf("%d",&a[i]),sum+=a[i];
            if(sum==0)
                break;
            sum=0;
            for(int i=0;i<6;i++)
            {
                sum+=a[i]*(i+1);//求总和  
                b[i]=i+1;
            }
            printf("Collection #%d:
    ",t++);
            if(sum%2==1)
            {
                printf("Can't be divided.
    
    "); //总和是奇数则不能平分 
                continue;
            }
            int num=sum/2;
            memset(dp,0,sizeof(dp));
            int to=0;
            for(int i=0;i<6;i++)
            {
                if(a[i]==0) continue;
                int te=1;
                while(a[i]>te)
                {
                    st[to++]=te*b[i];//将新的值赋给st[]  
                    a[i]-=te;
                    te*=2;//二进制压缩为——01背包  
                }
                st[to++]=a[i]*b[i];
            }
            for(int i=0;i<to;i++)//01背包
            {
                for(int j=num;j>=st[i];j--)
                    dp[j]=max(dp[j],dp[j-st[i]]+st[i]);
            }
            if(dp[num]==num)
                printf("Can be divided.
    
    ");
            else
                printf("Can't be divided.
    
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    Python中的list,tuple,dict,set
    linux上发布网站遇到的问题
    asp.net通过配置文件设置默认页
    半自动安装 linux 系统
    redis整合spring
    redis介绍。
    DAC模式登陆(解决对象名'sys.sysobjvalues'无效)
    对存储过程进行加密和解密(SQL 2008/SQL 2012)
    UML 系列
    Redis学习笔记~目录
  • 原文地址:https://www.cnblogs.com/nanfenggu/p/7899994.html
Copyright © 2020-2023  润新知