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. WPF学习 - 用鼠标移动、缩放、旋转图片(2)- 使用MatrixTransform

    在上一篇文章中,提到了以鼠标控制变换图片的方法. 这种方法在某种情况下可以,例如,直接在windows窗体上.但我发现,当把它封装到一个控件中的时候,它就不行了. 经过不断的尝试,我找到了一种更简单的 ...

  2. Visual Studio 2022 设置代码补全

    Visual Studio 2022 设置代码补全 VS默认使用 Tab 键进行代码补全. 若要使用回车补全需要重新设置,具体路径如下: ​ 工具----选项----文本编辑器----C/C++--- ...

  3. 使用GPU训练Pytorch模型

    如何使用GPU训练Pytorch模型 这两天的深度学习实验真实让人头疼,传说中的"猫狗大战",对模型的训练用CPU的话9h起步,12h是常态,大学生哪耗得起,因此查找资料搭建了GP ...

  4. 「にちじょう記録」MTIDnWtMOA

    Mistakes That I Don't Want to Make Once Again. // Caution // 差分 / 前缀和后注意询问区间端点有变化-- 不要考虑了右边界就不考虑左边界 ...

  5. Note -「SOS DP」高维前缀和

    本文差不多算是翻译了一遍 CF blog?id=45223 就是抄了一遍,看不懂可以去原文. 当然我的翻译并不是完全遵从原文的. Part. 1 Introduction 平时我们怎么求高维前缀和?容 ...

  6. ClickHouse(15)ClickHouse合并树MergeTree家族表引擎之GraphiteMergeTree详细解析

    GraphiteMergeTree该引擎用来对Graphite数据(图数据)进行瘦身及汇总.对于想使用ClickHouse来存储Graphite数据的开发者来说可能有用. 如果不需要对Graphite ...

  7. Vue element-ui 动态生成自定义table表头实现数据渲染

    需求:1)表头的数据是动态的,有可能字段值很长且很多.解决方案自定义动态表头,字段长使用文字提示[el-tooltip组件]: 2)需要对表格data中的数据值进行枚举转成中文值,且显示不同的颜色. ...

  8. ARM开发板学习

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

  9. 低代码助力微信小程序对接,提升开发效率

    摘要:本文由葡萄城技术团队原创并首发.转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 前言 微信小程序相信大家都用过,相较于APP,微信小程序的优势在于其便 ...

  10. Mind2Web: Towards a Generalist Agent for the Web 论文解读

    主页:https://osu-nlp-group.github.io/Mind2Web 训练集:https://huggingface.co/datasets/osunlp/Mind2Web 概要 本 ...