(Educational Codeforces Round 9)Thief in a Shop(dp)
2016-03-02 17:10
459 查看
Thief in a Shop
time limit per test5 secondsmemory 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 21 2 3
output
2 3 4 5 6input
5 51 1 1 1 1
output
5input
3 33 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; }
相关文章推荐
- 《Linux内核分析》第二周 操作系统是如何工作的?
- nginx下配置多站点
- android camera 架构图:参考图
- Linux信号(signal)
- eclipse tomcat maven 集成开发环境
- linux shell用法(7)-- grep命令
- linux存储技术与应用:Multipath多路径
- linux下删除-号开头的文件
- linux mp3播放及分析
- Linux下双网卡绑定bond0
- android 6.0架构下wpa_supplicant分析
- IIS服务管理器无法启动网站的解决方法
- Install YouTube-DL – A Command Line Video Download Tool for Linux
- IIS服务管理器无法启动网站的解决方法
- Linux IO模型
- Linux device tree 简要笔记
- Nginx支持比Apache高并发的原因
- linux安装mysq总监
- C语言中linux下查看sd卡mount的位置
- OpenCV学习13