zoukankan      html  css  js  c++  java
  • F

    F - Coins
    Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

    Description

    People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box 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
    



    多重背包,废话不多,看代码就OK了,
    #include<iostream>
    using namespace std;
    int dp[100005];
    int sum[100005];
    int coin[100],numofcoin[100];
    int main()
    {
        int n,m;
        int num;
        while(cin>>n>>m)
        {
            if(0==n && 0==m)
                break;
            for(int i=0;i<n;i++)
                cin>>coin[i];
            for(int i=0;i<n;i++)
                cin>>numofcoin[i];
            memset(dp,0,sizeof(dp));
            dp[0]=1;
            num=0;
            for(int i=0;i<n;i++)
            {
                memset(sum,0,sizeof(sum));
                for(int j=coin[i];j<=m;j++)
                {
                    if(!dp[j] && dp[j-coin[i]] && sum[j-coin[i]]<numofcoin[i])//
                    {
                        num++;
                        dp[j]=1;
                        sum[j]=sum[j-coin[i]]+1;
                    }
                }
            }
            cout<<num<<endl;
        }
    }
    当然,这个还可以化成一维背包来做,但是一维背包会超时,这时可以做一些优化,如15个1可以化成1,,2,4,8,0;
    这是我的代码,但是没有AC,目前还未解决
    #include<iostream>
    #include<math.h>
    using namespace std;
    int dp[100005];
    int coin[100],numofcoin[100];
    int numofGroups(int i)
    {
        int n=0;
        for(;pow(2.0,n)<numofcoin[i];n++);
        return n+1;
    }
    int main()
    {
        int n,m;
        int num;
        while(cin>>n>>m)
        {
            if(0==n && 0==m)
                break;
            for(int i=0;i<n;i++)
                cin>>coin[i];
            for(int i=0;i<n;i++)
                cin>>numofcoin[i];
            memset(dp,0,sizeof(dp));
            dp[0]=1;
            num=0;
            for(int i=0;i<n;i++)
            {
                int groups=numofGroups(i);
                for(int j=0;j<groups;j++)//一维背包
                {
                    int tem=0;
                    if(j==groups-1)
                        tem=numofcoin[i]-(int)pow(2.0,j)+1;
                    else
                        tem=(int)pow(2.0,j);
                    for(int k=m;k>=tem*coin[i];k--)
                    {
                        if(!dp[k] && dp[k-tem*coin[i]])
                        {
                            dp[k]=1;
                            num++;
                        }
                    }
                }
            }
            cout<<num<<endl;
        }
    }

  • 相关阅读:
    python面试题目
    Mysql综合练习作业50题
    django rest framework自定义返回格式
    mongodb副本集部署
    celery学习笔记
    npm学习笔记
    awk学习笔记
    SaltStack grains学习笔记
    nginx + uwsgi + centos7部署django
    Reids集群安装
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/3918314.html
Copyright © 2011-2022 走看看