支付对应的是多重背包问题,找零对应完全背包问题。

难点在于找上限T+maxv*maxv,可以用鸽笼原理证明,实在想不到就开一个尽量大的数组。

 1 #include  <map>
2 #include <set>
3 #include <cmath>
4 #include <queue>
5 #include <cstdio>
6 #include <vector>
7 #include <climits>
8 #include <cstring>
9 #include <cstdlib>
10 #include <iostream>
11 #include <algorithm>
12 using namespace std;
13 const int maxm=10000+120*120+5;
14 int dp_pay[maxm],dp_change[maxm];
15 int N,T,v[105],c[105];
16
17 void multi_knapsack(int n,int W){//多重背包,二进制拆分
18 memset(dp_pay,0x3f,sizeof(dp_pay));
19 dp_pay[0]=0;
20 for(int i=1;i<=N;i++){//转化为完全背包
21 if(c[i]*v[i]>=W){
22 for(int j=v[i];j<=W;j++)
23 dp_pay[j]=min(dp_pay[j],dp_pay[j-v[i]]+1);
24 }
25 else{
26 for(int k=1;c[i]>0;k<<=1){//二进制拆分
27 int x=min(k,c[i]);
28 for(int j=W;j>=v[i]*x;j--)
29 dp_pay[j]=min(dp_pay[j],dp_pay[j-v[i]*x]+x);
30 c[i]-=x;
31 }
32 }
33 }
34 }
35
36 void complete_knapsack(int n,int W){
37 memset(dp_change,0x3f,sizeof(dp_change));
38 dp_change[0]=0;
39 for(int i=1;i<=N;i++){
40 for(int j=v[i];j<=W;j++)
41 dp_change[j]=min(dp_change[j],dp_change[j-v[i]]+1);
42 }
43 }
44
45 int main(){
46 while(~scanf("%d%d",&N,&T)){
47 int maxv=0,W;
48 for(int i=1;i<=N;i++)
49 scanf("%d",&v[i]),maxv=max(maxv,v[i]);
50 for(int i=1;i<=N;i++)
51 scanf("%d",&c[i]);
52 maxv=maxv*maxv;
53 multi_knapsack(N,maxv+T);
54 complete_knapsack(N,maxv);
55 int ans=0x3f3f3f3f;
56 for(int i=0;i<=maxv;i++)
57 ans=min(ans,dp_pay[i+T]+dp_change[i]);
58 if(ans==0x3f3f3f3f)
59 ans=-1;
60 printf("%d\n",ans);
61 }
62 }

POJ3260 The Fewest Coins(混合背包)的更多相关文章

  1. POJ3260:The Fewest Coins(混合背包)

    Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he a ...

  2. POJ3260——The Fewest Coins(多重背包+完全背包)

    The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very eff ...

  3. POJ 3260 The Fewest Coins(多重背包+全然背包)

    POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...

  4. 洛谷P2851 [USACO06DEC]最少的硬币The Fewest Coins(完全背包+多重背包)

    题目描述 Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he always p ...

  5. poj3260 The Fewest Coins

    Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he a ...

  6. POJ 3260 The Fewest Coins(完全背包+多重背包=混合背包)

    题目代号:POJ 3260 题目链接:http://poj.org/problem?id=3260 The Fewest Coins Time Limit: 2000MS Memory Limit: ...

  7. POJ3260The Fewest Coins[背包]

    The Fewest Coins Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6299   Accepted: 1922 ...

  8. The Fewest Coins POJ - 3260

    The Fewest Coins POJ - 3260 完全背包+多重背包.基本思路是先通过背包分开求出"付出"指定数量钱和"找"指定数量钱时用的硬币数量最小值 ...

  9. HDU 3535 AreYouBusy (混合背包)

    题意:给你n组物品和自己有的价值s,每组有l个物品和有一种类型: 0:此组中最少选择一个 1:此组中最多选择一个 2:此组随便选 每种物品有两个值:是需要价值ci,可获得乐趣gi 问在满足条件的情况下 ...

随机推荐

  1. Go语言基础三:基本数据类型和运算符

    Go语言数据类型 与其他编程语言一样,Go语言提供了各种数据类型,可分为基本的数据类型和复杂的数据类型.基本的数据类型就是基本的构造块,例如字符串.数字和布尔值.复杂的数据类型是用户自己定义的结构,由 ...

  2. 操作表查询&操作表创建&操作表删除&操作表修改

    2.操作表 C(create):创建 语法: create table 表明( 列名1 数据类型1, 列名2 数据烈性2, .... 列名n 数据类型n ); create table Student ...

  3. super与this关键字图解和java继承的三个特点

    java继承的三个特点 java语言是单继承的 一个类的直接父类只能有一个 class A{} class B extends A{}//正确 class C{} class D extends A, ...

  4. CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-E

    比赛链接 A 题解 知识点:思维,模拟. 发现 \(b\) 串第一个字符是 \(1\) 则只能使用 max , \(0\) 则只能使用 min ,随后只需要模拟到 \(a\) 串剩余 \(m\) 个字 ...

  5. 结束语句之 continue

    C 语言自学之 continue Dome1:计算1到20之间不能被3整除的数字之和.               运算结果为: sum=147 1 #include<stdio.h> 2 ...

  6. SpringBoot 01 概述

    官方文档 https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/ 简介 SpringBoot 是一个 JavaWeb ...

  7. 浅谈MySQL的sql_mode

    SQL mode 今天我们来分享一下MySQL的SQL mode , 这也是我们比较容易忽略的一点,我们在一开始安装数据库的时候其实就要先考虑要保留哪些SQL mode,去除哪些,合理的配置能够减少很 ...

  8. 【Go实战基础】GO语言是什么,有哪些优势

    一.简介 2007年,为了提高在多核.网络机器(networked machines).大型代码库(codebases)的业务场景下的开发效率,Google 首席软件工程师决定创造一种语言那就是 Go ...

  9. Excel 笔记目录

    前言 Excel 是微软(Microsoft)公司推出的 Office 办公系列软件的一个重要组成部分,主要用于电子表格处理,可以高效地完成各种表格和图表的设计,进行复杂的数据计算和分析. 一句科普 ...

  10. QtCreator像C# region一样折叠代码

    C# #region "comment" [code] #endregion 就可以在VS中实现代码折叠了 QtCreator #pragma region "comme ...