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;


    }

    }

     
  • 相关阅读:
    杭电1171 Big Event in HDU(母函数+多重背包解法)
    怎样设计接口?
    未将对象引用设置到对象的实例--可能出现的问题总结
    開始Unity3D的学习之旅
    介绍一款轻量级js控件:easy.js
    Mustache 使用心得总结
    (ArcGIS API For Silverlight )QueryTask 跨层查询,和监控完整的查询!
    非常基本的SQL 内外连接
    Myeclipse它显示了一个目录的结构,而不是包
    Duanxx的Design abroad: C++矩阵运算库Eigen 概要
  • 原文地址:https://www.cnblogs.com/llsq/p/5957527.html
Copyright © 2011-2022 走看看