codeforces632E. Thief in a Shop (dp)
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 2
1 2 3
2 3 4 5 6
5 5
1 1 1 1 1
5
3 3
3 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 1000000007
using 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)的更多相关文章
- C - Thief in a Shop - dp完全背包-FFT生成函数
		
C - Thief in a Shop 思路 :严格的控制好k的这个数量,这就是个裸完全背包问题.(复杂度最极端会到1e9) 他们随意原来随意组合的方案,与他们都减去 最小的 一个 a[ i ] 组合 ...
 - Educational Codeforces Round 9 E. Thief in a Shop dp fft
		
E. Thief in a Shop 题目连接: http://www.codeforces.com/contest/632/problem/E Description A thief made hi ...
 - Codeforces632E Thief in a Shop(NTT + 快速幂)
		
题目 Source http://codeforces.com/contest/632/problem/E Description A thief made his way to a shop. As ...
 - 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 ...
 - codeforces Educational Codeforces Round 9  E - Thief in a Shop
		
E - Thief in a Shop 题目大意:给你n ( n <= 1000)个物品每个物品的价值为ai (ai <= 1000),你只能恰好取k个物品,问你能组成哪些价值. 思路:我 ...
 - CF632E Thief in a Shop 和 CF958F3 Lightsabers (hard)
		
Thief in a Shop n个物品每个价值ai,要求选k个,可以重复.问能取到哪几个价值? 1 ≤ n, k ≤ 1000,1 ≤ ai ≤ 1000 题解 将选一个物品能取到的价值的01生成函 ...
 - codeforces 632E. Thief in a Shop   fft
		
题目链接 E. Thief in a Shop time limit per test 5 seconds memory limit per test 512 megabytes input stan ...
 - Educational Codeforces Round 9 E. Thief in a Shop NTT
		
E. Thief in a Shop A thief made his way to a shop. As usual he has his lucky knapsack with him. Th ...
 - CF632E: Thief in a Shop(快速幂+NTT)(存疑)
		
A thief made his way to a shop. As usual he has his lucky knapsack with him. The knapsack can contai ...
 
随机推荐
- docker 删除和拉取镜像
			
删除镜像 # docker rmi -f 镜像id # 删除指定镜像 docker rmi -f 25d5f6s564 # docker rmi -f 镜像id 镜像id # 删除多个镜像 docke ...
 - innodb是怎么刷新日志缓冲的
			
当innodb把日志缓冲刷新到磁盘日志文件的时候,先会用一个mutex锁住缓冲区,刷新到所需要的位置,然后移动剩下的条目到缓冲区的前面,当mutex释放时,可能有超过一个事务已经准备好刷新其日志记录, ...
 - 【Linux】zabbix4.0服务器搭建,agent搭建,及邮件使用方法
			
zabbix默认的 服务端监听端口为10051,而被监控端即Zabbix--agents代理程序监控10050端口. 更新yum源: yum clean all yum makecache 需要配置网 ...
 - Python批量 png转ico
			
Python 批量 png 转 ico 一.前言: 首先说一下ico文件的作用:ico是windows的图标文件格式,可以用于浏览器首段图标显示,也可以用于Windows软件.我的话一般用来美化文件夹 ...
 - VBScript调用winscp,实现sftp操作
			
最新有一个需求,需要在ssis中调用sftp下载文件,由于服务器上只有framework2.0,并且需要用sqlserver代理调用作业,限制了很多. 首先用的是脚本任务,进程调用winscp.com ...
 - Redis 实战 —— 02. Redis 简单实践 - 文章投票
			
需求 功能: P15 发布文章 获取文章 文章分组 投支持票 数值及限制条件 P15 如果一篇文章获得了至少 200 张支持票,那么这篇文章就是一篇有趣的文章 如果这个网站每天有 50 篇有趣的文章, ...
 - HTML基础复习3
			
CSS 可以理解为对HTML的一种补充 CSS由两部分组成:选择器.声明,声明中包含属性和值 CSS中的选择器 HTML标签选择器 类选择器 在标签上使用class属性为标签起个类名,在CSS中使用. ...
 - 微人事项目-mybatis-持久层
			
摘要 最近将微人事这个开源项目进行了复现,这篇文章记录mybaits访问数据库这一块. 其中MyBatis是一个流行的持久层框架,支持自定义SQL.存储过程和高级映射.MyBatis消除了几乎所有的J ...
 - day131:2RenMJ:2RenMJ游戏简介&部署MJ项目到本地
			
目录 1.游戏简介 1.如何做出一款麻将游戏? 2.麻将运行界面 3.麻将项目所用技术快速概览 4.web开发 / 游戏开发 / APP开发 比较 5.firefly游戏框架介绍 2.部署麻将项目到本 ...
 - ovs-ofctl命令
			
用于监控和管理 OpenFlow 交换机. 1. 交换机管理命令 查看交换机信息: ovs-ofctl show s1 查看交换机流表: ovs-ofctl dump-tables s1 查看端口信 ...