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;
        }
    }

  • 相关阅读:
    最新的thinkphp 数据库字段 自动转为小写。是个坑,要小心
    游戏助手开发
    最新的thinkphp 后台入口的问题
    最近的小成就
    一个大坑1111111111
    一个大坑
    代码的重构
    PHP上传文件大小的修改
    博客园Markdown编辑器代码高亮失效(官方已修复)
    2014年最新前端开发面试题(题目列表+答案 完整版)
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/3916890.html
Copyright © 2011-2022 走看看