zoukankan      html  css  js  c++  java
  • F

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

    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


    多从背包问题,一开始没用多重背包,而是用遍历和hash结合写了一个,那么自然是超时了
    当然了,发现自己没有很好的理解了动态规划。
    动态规划主要用于可以将问题变为一系列的子问题的最优化处理,另一方面它其实是实现一个记录的作用,这样就不必去重新求解以前求过的问题,而直接用就行了。那么当然就更快乐。
    (一超时代码)#include<iostream>
    using namespace std;
    short int a[100005];//hash数组,用来判断这个硬币数是否存在了
    int b[100005];//用来记录已有的coin值的和
    int coin[100];
    int num[100];
    int main()
    {
        int n,m;
        int zmax;//当前的最大硬币和
        int endnum;//结果个数
        while(cin>>n>>m)
        {
            if(0==n && 0==m)
                break;
            zmax=0;
            endnum=0;
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
            for(int i=0;i<n;i++)
                cin>>coin[i];
            for(int i=0;i<n;i++)
                cin>>num[i];
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<num[i];j++)t6

                {
                    for(int z=endnum;z>=0;z--)
                    {
                        int tem=coin[i]+b[z];
                        if(tem<=m && 0==a[tem])//
                        {
                            endnum++;
                            b[endnum]=tem;
                            a[tem]=1;
                        }
                    }
                }
            }
            cout<<endnum<<endl;
        }
    }

  • 相关阅读:
    Spring Boot入门教程1、使用Spring Boot构建第一个Web应用程序
    单点登录(SSO)的设计
    .NET Core快速入门教程 5、使用VS Code进行C#代码调试的技巧
    .NET Core快速入门教程 4、使用VS Code开发.NET Core控制台应用程序
    .NET Core快速入门教程 3、我的第一个.NET Core App (CentOS篇)
    .NET Core快速入门教程 2、我的第一个.NET Core App(Windows篇)
    .NET Core快速入门教程 1、开篇:说说.NET Core的那些事儿
    JAVA的8种基本数据类型
    JVM
    在coffeescript中声明和屏蔽模块变量
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/3916890.html
Copyright © 2011-2022 走看看