(Educational Codeforces Round 9)Thief in a Shop(dp)
来源:互联网 发布:吉利研究院 知乎 编辑:程序博客网 时间:2024/06/08 22:27
Thief in a Shop
time limit per test5 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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个数,然后这n个数里面选k个加起来
问你一共能加出来多少种
题解:
多项式加法,加k次,问你最后的数是哪些。
DP。dp[i]表示最少用多少个非a[1]能够构成a[1]*k+i的。
#include<bits/stdc++.h>using namespace std;int a[1010], dp[1000005], done[1010]={0};int main(){ int mod = 1e9; fill( dp + 1, dp + 1000001 , mod); int n ,k, mn = mod, mx = 0; cin >> n >> k; for(int i=1;i<=n;++i) { cin >> a[i]; mn = min( mn , a[i]); mx = max( mx , a[i]); } for(int i=1;i<=n;++i) a[i] -= mn; int t = 0; for(int i=1;i<=n;++i) if( a[i] != 0) a[++t] = a[i]; n = t; dp[0]=0; for(int i = 1; i <= n;++i){ if( done[a[i]]) continue; done[a[i]] = 1; for(int j = 1; j<=mx*k;++j){ if(j>=a[i]) if(dp[j] > dp[j-a[i]] + 1) dp[j] = dp[j-a[i]] + 1; } } for(int i = 0; i <= mx * k; ++i) if( dp[i] <= k) printf("%d ",mn * k + i); return 0;}
- (Educational Codeforces Round 9)Thief in a Shop(dp)
- Educational Codeforces Round 9 E. Thief in a Shop(FFT)
- Educational Codeforces Round 9 E.Thief in a Shop
- Educational Codeforces Round 9 E. Thief in a Shop
- Educational Codeforces Round 9 E.Thief in a Shop (FFT)★ ★
- Educational Codeforces Round 9 E. Thief in a Shop (FFT,计数)
- CodeForces 632E Thief in a Shop(DP)
- codeforces_632E.Thief in a Shop(dp)
- CodeForces 632E Thief in a Shop(DP|完全背包)
- CodeForces 632E Thief in a Shop(FFT)
- Thief in a Shop CodeForces
- codeforces632E. Thief in a Shop (dp)
- Educational Codeforces Round 16 -- E. Generate a String (DP)
- Educational Codeforces Round 16 E. Generate a String (dp)
- Codeforces 632E Thief in a Shop(FFT+快速幂)
- (Educational Codeforces Round 9)Longest Subsequence(dp)
- Educational Codeforces Round 6 (A)贪心
- Educational Codeforces Round 7(A)数学
- 性能测试(三):性能测试怎么调优
- AOP中获得自定义注解对象
- jQuery的ready()事件与js中的onload事件的区别
- Java中SimpleDateFormat类parse解析yyyy-MM-dd与yyyy-M-d是否可以混用
- css优化text输入框代码的解析
- (Educational Codeforces Round 9)Thief in a Shop(dp)
- Apple pay的证书问题
- UISearchBar改变输入框的背景颜色
- Qt与MSVC中文乱码问题的解决方案
- hdu 2821
- 性能测试(四):性能测试如何写报告
- IOS设置完定位后回到APP中后要自动做某些操作的方法,使用通知
- Android中Matrix的学习
- 如何在一台windows的服务器里安装配置PHP+MYSQL环境