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. O2OA(翱途)开发平台 V8.1正式发布

    尊敬的O2OA(翱途)平台合作伙伴.用户以及亲爱的开发小伙伴们,平台 V8.1版本已正式发布.正值8月的最后一周,我们以更安全.更高效.更好用的崭新面貌迎接9月的到来. O2OA开发平台v8.1版本更 ...

  2. PC首页资源加载速度由8s降到2s的优化实践

    随着需求的不断开发,前端项目不断膨胀,业务提出:你们的首页加载也太慢啦,我都需要7.8秒才能看到内容,于是乎主管就让我联合后端开启优化专项,目标是3s内展示完全首页的内容. 性能指标 开启优化时,我们 ...

  3. 如何在Vue3中配置国际化语言i18n

    1. 安装 vue-i18n npm i vue-i18n -S 2. 创建一个i8n的配置文件 如:i18nConfig.js // 配置 vue-i18n 实现国际化语言设置 import { c ...

  4. 记一次 .NET 某电力系统 内存暴涨分析

    一:背景 1. 讲故事 前些天有位朋友找到我,说他生产上的程序有内存暴涨情况,让我帮忙看下怎么回事,最简单粗暴的方法就是让朋友在内存暴涨的时候抓一个dump下来,看一看大概就知道咋回事了. 二:Win ...

  5. java开发面试笔记

    目录 1.hashMap hashmap源码分析-逐行注释版: 2.线程池 3.MySQL数据库引擎.事务.锁机制 1. 引擎 2. 事务 索引 3. 锁: 4. 调优问题怎么回答? 4.SQL语句 ...

  6. Go 1.22 中的 For 循环

    原文在这里. 由 David Chase and Russ Cox 发布于2023年9月19日 Go 1.21 版本包含了对 for 循环作用域的预览更改,我们计划在 Go 1.22 中发布此更改,以 ...

  7. 从MVC到DDD,该如何下手重构?

    作者:付政委 博客:bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 大家好,我是技术UP主小傅哥.多年的 DDD 应用,使我开了技术的眼界! MVC 旧工程腐化严重,迭代成本太高 ...

  8. ARM开发板学习

    ARM开发板学习 1.蜂鸣器配饰和时间函数开发 #include <stdio.h> #include <wiringPi.h> #include <unistd.h&g ...

  9. Linux系列教程——Linux磁盘管理、Linux进程管理、Linux系统服务、 Linux计划任务

    @ 目录 1 Linux磁盘管理 1.磁盘的基本概念 1.什么是磁盘 2.磁盘的基本结构 3.磁盘的预备知识 1.磁盘的接口类型 2.磁盘的基本术语 3.磁盘在系统上的命名方式 4.磁盘基本分区Fdi ...

  10. Python shape+size详解

    import cv2 from PIL import Image # pic.JPG 图片的路径 img = cv2.imread("pic.JPG",-1) print(&quo ...