codeforces632E. Thief in a Shop (dp)
来源:互联网 发布:windows 平板 编辑:程序博客网 时间:2024/05/29 04:28
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.
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.
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.
3 21 2 3
2 3 4 5 6
5 51 1 1 1 1
5
3 33 5 11
9 11 13 15 17 19 21 25 27 33
题意:给你n种物品以及每种的价值,每一种物品可以任意取多次,问恰好取k次物品能取到的所有可能价值。
思路:容易想到4维dp,用dp[i][j]表示取i次,价值为j是否存在,但是这样的复杂度为10^12爆了,所以要减少一维,先对n个数排序,然后n个数都减去第一个数(这样做的目的是恰好k次很难dp,n个数都减去最小的数后,第1个数就变为0,在这样的情况下,如果我们凑到价值为i的物品少于k件,如只用k-2件拼凑,那么另外两件可以看做都用第1个物品),然后用dp[i]表示最后取到价值为i的物品,dp就行了。
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<string>#include<algorithm>#define inf 99999999#define pi acos(-1.0)#define maxn 1005#define MOD 1000000007using namespace std;typedef long long ll;typedef long double ldb;int dp[maxn*maxn],a[maxn];int main(){ int n,m,i,j,k; while(scanf("%d%d",&n,&k)!=EOF) { for(i=1;i<=n;i++){ scanf("%d",&a[i]); } sort(a+1,a+1+n); int t=a[1]; for(i=1;i<=n;i++){ a[i]-=t; } for(i=0;i<=1000000;i++)dp[i]=inf; dp[0]=0; for(j=0;j<=1000000;j++){ for(i=1;i<=n;i++){ if(j>=a[i]){ dp[j]=min(dp[j],dp[j-a[i] ]+1); } } } int flag=1; for(j=0;j<=1000000;j++){ if(dp[j]<=k){ printf("%d ",j+t*k); } } printf("\n"); } return 0;}
- codeforces632E. Thief in a Shop (dp)
- codeforces632e Thief in a Shop(完全背包)
- codeforces_632E.Thief in a Shop(dp)
- CodeForces 632E Thief in a Shop(DP)
- Thief in a Shop CodeForces
- CF 632E Thief in a Shop
- (Educational Codeforces Round 9)Thief in a Shop(dp)
- CodeForces 632E Thief in a Shop(DP|完全背包)
- Educational Codeforces Round 9 E.Thief in a Shop
- CodeForces 632E Thief in a Shop(FFT)
- Codeforces 632E Thief in a Shop(FFT+快速幂)
- 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)★ ★
- Educational Codeforces Round 9 E. Thief in a Shop (FFT,计数)
- SGU 482 Impudent Thief dp
- 377. Set a thief to catch a thief. 以贼捉贼
- 377. Set a thief to catch a thief. 以贼捉贼
- 【求助】js到处Excel window.btoa IE 下没有定义
- C/C++ 文件路径解析
- jsp导入的包,包里明明有方法,但总是报方法没有定义的错
- 深刻的理解递归汉诺塔(Hanoi)
- Java学习整理系列之ThreadLocal的理解
- codeforces632E. Thief in a Shop (dp)
- Spring Cache
- 关于C++内存中字节对齐问题的详细介绍
- php include文件的编码影响ajax接收
- B. Far Relative’s Problem
- Arduino通信协议设计
- 屏幕适配之-Autoresizing
- .net2.0的DirectX.dll和Directsound.dll怎么使用在.net4.0上。
- Swift - 人脸检测,以及人脸打码