The trouble of Xiaoqian

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2277    Accepted Submission(s): 805

Problem Description
In the country of ALPC , Xiaoqian is a very famous mathematician. She is immersed in calculate, and she want to use the minimum number of coins in every shopping. (The numbers of the shopping include the coins she gave the store and the store backed to her.)
And now , Xiaoqian wants to buy T (1 ≤ T ≤ 10,000) cents of supplies. The currency system has N (1 ≤ N ≤ 100) different coins, with values V1, V2, ..., VN (1 ≤ Vi ≤ 120). Xiaoqian is carrying C1 coins of value V1, C2 coins of value V2, ...., and CN coins of value VN (0 ≤ Ci ≤ 10,000). The shopkeeper has an unlimited supply of all the coins, and always makes change in the most efficient manner .But Xiaoqian is a low-pitched girl , she wouldn’t like giving out more than 20000 once.
Input
There are several test cases in the input.
Line 1: Two space-separated integers: N and T.
Line 2: N space-separated integers, respectively V1, V2, ..., VN coins (V1, ...VN)
Line 3: N space-separated integers, respectively C1, C2, ..., CN
The end of the input is a double 0.
Output
Output one line for each test case like this ”Case X: Y” : X presents the Xth test case and Y presents the minimum number of coins . If it is impossible to pay and receive exact change, output -1.
Sample Input
3 70
5 25 50
5 2 1
0 0
Sample Output
Case 1: 3
题目大意就是小倩付多少钱才会有交换的硬币次数最少。
这是一道混合背包题目,小倩的硬币是多重背包,售货员的是完全背包。因为不会超过20000元;那么遍历就好了
AC代码:
 
 
 1 #include<iostream>
2 #include<stdio.h>
3 #include<algorithm>
4 #include<string.h>
5 #include<string>
6 #define INF 9999999
7 #include<algorithm>
8 using namespace std;
9 int dp1[20005],dp2[20005];
10 int val[150],num[150];
11 int min(int x,int y)
12 {
13 return x>y? y:x;
14 }
15 int main()
16 {
17 int n,m;
18 int T=1;
19 while(cin>>n>>m)
20 {
21 if(n==0&&m==0) break;
22 for(int i=1;i<=n;i++)
23 cin>>val[i];
24 for(int i=1;i<=n;i++)
25 cin>>num[i];
26 for(int i=1;i<=20000;i++)
27 dp1[i]=dp2[i]=INF;
28 dp1[0]=dp2[0]=0;
29 for(int i=1;i<=n;i++)//多重背包
30 {
31 for(int k=1,flag=0;;k*=2)
32 {
33 if(k*2>num[i])
34 {
35 k=num[i]-k+1;
36 flag=1;
37 }
38 for(int j=20000;j>=k*val[i];j--)
39 {
40 dp1[j]=min(dp1[j],dp1[j-k*val[i]]+k);
41 }
42 if(flag)
43 break;
44 }
45 }
46 for(int i=1;i<=n;i++)//完全背包,这里有两种写法,一种是这种常规的优化了的,还有一种适合新手,虽然复杂度会高些,但是新手理解起来会更容易。
47 {
48 for(int j=val[i];j<=20000;j++)49 dp2[j]=min(dp2[j],dp2[j-val[i]]+1);
50 }
       /*
        for(int i=1;i<=n;i++)
       {
          for(int k=1;k*val[i]<20000;k*=2)//类比于多重背包
   {
     for(int j=20000;j>=k*val[i];j--)
     {
    dp2[j]=min(dp2[j],dp2[j-k*val[i]]+k);
    }  
     }
     }
          */

51 int lss=INF;
52 for(int i=m;i<=20000;i++)
53 {
54 lss=min(lss,dp1[i]+dp2[i-m]);
55 }
56 if(lss>20000)
57 cout<<"Case "<<T++<<": "<<-1<<endl;
58 else
59 cout<<"Case "<<T++<<": "<<lss<<endl;
60 }
61 return 0;
62 }
 

HDU-3591 混合背包的更多相关文章

  1. HDU 3591 (完全背包+二进制优化的多重背包)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3591 The trouble of Xiaoqian Time Limit: 2000/1000 M ...

  2. hdu 2844 混合背包【背包dp】

    http://acm.hdu.edu.cn/showproblem.php?pid=2844 题意:有n种纸币面额(a1,a2,...an),每种面额对应有(c1,c2,...cn)张.问这些钱能拼成 ...

  3. HDU 3591 多重背包

    给出N种钱币和M 给出N种钱币的面值和个数 NPC拿着这N些钱币去买价值M的物品,能够多付.然后被找零,找零的钱也为这些面值.但没有数量限制 问最少经手的钱币数量 对于NPC做一个付款多重背包 然后对 ...

  4. HDU 2844 混合背包、

    题意:一个人想买手表,给你n个价值的硬币,然后给你n个价值硬币对应的个数.但是呢,这个人只知道这个手表的价格不超过m元.问他最多能买多少种价值的手表 思路:dp背包专题 但是- - 一直不知道该怎么d ...

  5. HDU 3535 分组混合背包

    http://acm.hdu.edu.cn/showproblem.php?pid=3535 题意:有n组工作,T时间,每个工作组中有m个工作,改组分类是s,s是0是组内至少要做一件,是1时最多做一件 ...

  6. HDU 3535 AreYouBusy(混合背包)

    HDU3535 AreYouBusy(混合背包) http://acm.hdu.edu.cn/showproblem.php?pid=3535 题意: 给你n个工作集合,给你T的时间去做它们.给你m和 ...

  7. HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)

    HDU 3591 The trouble of Xiaoqian(多重背包+全然背包) pid=3591">http://acm.hdu.edu.cn/showproblem.php? ...

  8. HDU 3535 AreYouBusy (混合背包)

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

  9. hdu 3591 The trouble of Xiaoqian

    hdu 3591  The trouble of Xiaoqian 题意:xiaoqi要买一个T元的东西,当前的货币有N种,xiaoqi对于每种货币有Ci个:题中定义了最小数量即xiaoqi拿去买东西 ...

  10. Codevs 3269 混合背包(二进制优化)

    3269 混合背包 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description 背包体积为V ,给出N个物品,每个物品占用体积为V ...

随机推荐

  1. mall :rabbit项目源码解析

    目录 一.mall开源项目 1.1 来源 1.2 项目转移 1.3 项目克隆 二.RabbitMQ 消息中间件 2.1 rabbit简介 2.2 分布式后端项目的使用流程 2.3 分布式后端项目的使用 ...

  2. Codeforces 1463D Pairs

    题意 对于数字\(1\)~\(2n\),可以构造出\(n\)个二元组,对于\(n\)个二元组,选择一个数组\(x\),留下\(x\)个二元组的最小值,留下\(n-x\)个二元组的最大值,其构成了一个集 ...

  3. linux shell根据关键字用sed注释掉整行

    一.将带有ab的行注释掉 # cat test # sed -i '/ab/s/^\(.*\)$/#\1/g' test ab是关键字 s是语法替换 ^是行首 $是行尾 \是转义符 数字1带表前述匹配 ...

  4. 用OLED屏幕播放视频(3): 使用cuda编程加速视频处理

    下面的系列文章记录了如何使用一块linux开发扳和一块OLED屏幕实现视频的播放: 项目介绍 为OLED屏幕开发I2C驱动 使用cuda编程加速视频处理 这是此系列文章的第3篇, 主要总结和记录了如何 ...

  5. 为什么大家都在用 WebP?

    WebP 是谷歌在 2010 年提出的一种新型的图片格式,放到现在来讲,已经不算是"新"技术了,毕竟已经有了更新的 JPEG XL 和 AVIF .但是在日常工作中,大家时常会碰到 ...

  6. k8s1.25版本上实践 Ingress-nginx

    背景: 领导要求的最新最新版本k8s...使用ingress-nginx 对外暴露内部服务 环境 节点 master worker 主机/ip calico-master01/192.168.195. ...

  7. Unexpected keys "@@dva", "user" found in preloadedState argument passed to createStore. Expected to find one of the known reducer keys instead: "router", "loading". Unexpected keys will be ignored.

    Please use `require("history").DOMUtils` instead of `require("history/DOMUtils") ...

  8. 轻松掌握组件启动之Redis单机、主从、哨兵、集群配置

    单机配置启动 Redis安装 下载地址:http://redis.io/download 安装步骤: 1: 安装gcc编译器:yum install gcc 2: 将下载好的redis‐5.0.3.t ...

  9. 银河麒麟V10 修改文件夹权限

    并不建议修改系统文件夹的权限,防止终端失效 指令:获取所有权限 指令:写入可执行权限 chmod +x filename//filename 是文件路径 TRANSLATE with x Englis ...

  10. P4156 [WC2016]论战捆竹竿 题解

    题目链接 题意描述 给定一个字符串 \(s\),你初始拥有一个空串 \(t\),每次可以选择这个字符串的一个 Border,去掉它后接在 \(t\) 的后面,操作后 \(s\) 不变,给出一个上限 \ ...