zoukankan      html  css  js  c++  java
  • codeforces 632+ E. Thief in a Shop

    E. Thief in a Shop
    time limit per test
    5 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    A thief made his way to a shop.

    As usual he has his lucky knapsack with him. The knapsack can contain k objects. There are n kinds of products in the shop and an infinite number of products of each kind. The cost of one product of kind i is ai.

    The thief is greedy, so he will take exactly k products (it's possible for some kinds to take several products of that kind).

    Find all the possible total costs of products the thief can nick into his knapsack.

    Input

    The first line contains two integers n and k (1 ≤ n, k ≤ 1000) — the number of kinds of products and the number of products the thief will take.

    The second line contains n integers ai (1 ≤ ai ≤ 1000) — the costs of products for kinds from 1 to n.

    Output

    Print the only line with all the possible total costs of stolen products, separated by a space. The numbers should be printed in the ascending order.

    Examples
    input
    3 2
    1 2 3
    output
    2 3 4 5 6
    input
    5 5
    1 1 1 1 1
    output
    5
    input
    3 3
    3 5 11
    output
    9 11 13 15 17 19 21 25 27 33

    题目大意:给你n个数,让你选k次,可重复选,输出所有可能值。
    思路:如果简单的用个二维dp[i][j],表示选i次价值为j是否存在,但这样显然会爆,10^12,那么改良下,用do[i]表示价值为i时,最少选几次,剩余的次数可以让其中的最小值去补。
    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int dp[1000006],s[1003];
    const int INF=0x3f3f3f3f;
    const int MAX=999999;
    int main()
    { int n,k;
    while(cin>>n>>k)
    {
    for(int i=0;i<n;i++)
    scanf("%d",&s[i]);
    sort(s,s+n);
    int t=s[0];
    for(int i=0;i<n;i++)
    s[i]-=t;
    for(int i=0;i<=k*s[n-1];i++)
    dp[i]=k+1;
    dp[0]=0;
    for(int i=0;i<=k*s[n-1];i++)
    {
    for(int j=0;j<n;j++)
    if(i>=s[j])
    dp[i]=min(dp[i-s[j]]+1,dp[i]);
    }
    for(int i=0;i<=k*s[n-1];i++)
    if(dp[i]<=k)
    cout<<i+k*t<<" ";
    cout<<endl;


    }

    }

     
  • 相关阅读:
    office(Word、Excel、PPT等图标异常和桌面无新建解决方案)
    CentOS7安装搭建.Net Core 2.0环境-详细步骤
    Win7系统system进程句柄数一直增加解决方案
    安装.Net Framework 4.6.2时出现“无法建立到信任根颁发机构的证书链”解决方法
    css关于定位那些事情
    1份2015计划书
    js关于弹也遮罩层
    每日一句(2014-10-23)
    每日一句(2014-10-17)
    每日一句(2014-10-16)
  • 原文地址:https://www.cnblogs.com/llsq/p/5957527.html
Copyright © 2011-2022 走看看