• 100 邮票


    100 邮票

    作者: Turbo时间限制: 1S章节: 动态规划

    问题描述 :

    已知一个 N 枚邮票的面值集合(如,{1 分,3 分})和一个上限 K ,表示信封上能够贴 K 张邮票。计算从 1 到 M 的最大连续可贴出的邮资。 例如,假设有 1 分和 3 分的邮票;你最多可以贴 5 张邮票。很容易贴出 1 到 5 分的邮资(用 1 分邮票贴就行了),接下来的邮资也不难:

    6 = 3 + 3

    7 = 3 + 3 + 1

    8 = 3 + 3 + 1 + 1

    9 = 3 + 3 + 3

    10 = 3 + 3 + 3 + 1

    11 = 3 + 3 + 3 + 1 + 1

    12 = 3 + 3 + 3 + 3

    13 = 3 + 3 + 3 + 3 + 1。

    然而,使用 5 枚 1 分或者 3 分的邮票根本不可能贴出 14 分的邮资。因此,对于这两种邮票的集合和上限 K=5,答案是 M=13。

    输入说明 :

    第 1 行: 两个整数,K 和 N。

    K(1 <= K <= 200)是可用的邮票总数。N(1 <= N <= 50)是邮票面值的数量。

    第 2 行 到最后: N 个整数,每行 最多15 个,列出所有的 N 个邮票的面值,面值不超过 10000。

    输出说明 :

    第 1 行: 一个整数,从 1 分开始连续的可用集合中不多于 K 张邮票贴出的邮资数。

    输入范例 :
    5 2
    1 3
    输出范例 :
    13

    #include <iostream>
    #include <algorithm>
    using namespace std;
    int dp[2000001];
    int k, n;
    int p[51];
    int main()
    {
    	cin >> k >> n;
    	for (int i = 0; i < n; i++)
    	{
    		cin>>p[i];
    	}
            sort(p,p+n);
            dp[0]=0;
    	int index = 0;
    	while (dp[index] <= k)
    	{
    		int min = 10000000;
    		index++;
    		for (int i = 0; i < n&&p[i]<=index; i++)
    		{
    			if (dp[index-p[i]] + 1 < min)
    				min = dp[index-p[i]] + 1;
    		}
    		dp[index] = min;
    	}
    	cout << index - 1 << endl;
    	return 0;
    }
    
    Yesterday is history,tomorrow ismystery,but today is a gift!That why it is called Present!
  • 相关阅读:
    output (Elements) – HTML 中文开发手册
    JavaSE IO类层次关系和Java IO流的用法总结
    PHP attributes() 函数
    math_errhandling (Numerics) – C 中文开发手册
    C while 循环
    HTML <a> hreflang 属性
    static_assert (Error handling) – C 中文开发手册
    C 嵌套 switch 语句
    HTML DOM Input Time name 属性
    Bootstrap 弹出框
  • 原文地址:https://www.cnblogs.com/VictorierJwr/p/12878530.html
Copyright © 2020-2023  润新知