The trouble of Xiaoqian
The trouble of Xiaoqian
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1472 Accepted Submission(s): 502
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
背包比较好的题,对于xiaoqian想要使经手的钱做少,所以它给店员的钱必定要T<=m<=20000,所以对于xiaoqian进行多重背包,计算付出m时,需要支付钱的数量最小值,对于店员,xiaoqian多给的钱需要找回,找回的钱的数量也要最少,店员的钱是没有限制的所以对店员进行完全背包
最后遍历找最小值
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAX = 20001;
int dp[MAX];
int Dp[MAX];
int w[120];
int c[120];
int MIN;
int main()
{
int n,m;
int W=1;
while(scanf("%d %d",&n,&m)&&(n||m))
{
for(int i=0;i<n;i++)
{
scanf("%d",&w[i]);
}
for(int i=0;i<n;i++)
{
scanf("%d",&c[i]);
}
memset(Dp,INF,sizeof(Dp));
memset(dp,INF,sizeof(dp));
Dp[0]=0;
dp[0]=0;
for(int i=0;i<n;i++)
{
int bite=1;
int num=c[i];
while(num)
{
num-=bite;
for(int j=MAX-1;j>=w[i]*bite;j--)
{
Dp[j]=min(Dp[j],Dp[j-w[i]*bite]+bite);
}
if(bite*2<num)
{
bite*=2;
}
else
{
bite=num;
}
}
}
for(int i=0;i<n;i++)
{
for(int j=w[i];j<MAX;j++)
{
dp[j]=min(dp[j],dp[j-w[i]]+1);
}
}
MIN=INF;
for(int i=m;i<MAX;i++)
{
MIN=min(MIN,Dp[i]+dp[i-m]);
}
printf("Case %d: ",W++);
if(MIN==INF)
{
printf("-1\n");
}
else
{
printf("%d\n",MIN);
}
}
return 0;
}
The trouble of Xiaoqian的更多相关文章
- hdu 3591 The trouble of Xiaoqian
hdu 3591 The trouble of Xiaoqian 题意:xiaoqi要买一个T元的东西,当前的货币有N种,xiaoqi对于每种货币有Ci个:题中定义了最小数量即xiaoqi拿去买东西 ...
- HDUOJ-----3591The trouble of Xiaoqian
The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)
HDU 3591 The trouble of Xiaoqian(多重背包+全然背包) pid=3591">http://acm.hdu.edu.cn/showproblem.php? ...
- HDU 3594 The trouble of Xiaoqian 混合背包问题
The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- hdu3591The trouble of Xiaoqian 多重背包+全然背包
//给出Xiaoqian的钱币的价值和其身上有的每种钱的个数 //商家的每种钱的个数是无穷,xiaoqian一次最多付20000 //问如何付钱交易中钱币的个数最少 //Xiaoqian是多重背包 / ...
- HDU - 3591 The trouble of Xiaoqian 题解
题目大意 有 \(N\) 种不同面值的硬币,分别给出每种硬币的面值 \(v_i\) 和数量 \(c_i\).同时,售货员每种硬币数量都是无限的,用来找零. 要买价格为 \(T\) 的商品,求在交易中最 ...
- hdu 3591 多重加完全DP
题目: The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- HDU 3591 (完全背包+二进制优化的多重背包)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3591 The trouble of Xiaoqian Time Limit: 2000/1000 M ...
- HDU_3591_(多重背包+完全背包)
The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
随机推荐
- mysql之innodb_buffer_pool
1>.mysqld重启之后,innodb_buffer_pool几乎是空的,没有任何的缓存数据.随着sql语句的执行,table中的数据以及index 逐渐被填充到buffer pool里面,之 ...
- swift语言实战晋级-第9章 游戏实战-跑酷熊猫-7-8 移动平台的算法
在上个小节,我们完成了平台的产生.那么我们来实现一下让平台移动.平台的移动,我们只需要在平台工厂类中写好移动的方法,然后在GameScene类中统一控制就行了. 在GameScene类中,有个upda ...
- 创建Java类并实例化深入理解
package com.sanguosha.java; import java.util.Scanner;//导入包 public class TestPerson { public static v ...
- 转:python webdriver API 之定位一组对象
webdriver 可以很方便的使用 find_element 方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,WebElement 接口同样提供了定位一组元素的方法 find_eleme ...
- WebService的简单应用
具体看项目源文件:包含: ip地址查询, QQ在线状态查询,和自定义的MD5 破解和加密(呵呵有形无势...) http://pan.baidu.com/s/1bn09WQj SOAP 1.1 The ...
- c++必读
下面的是学c++时要注意的.绝对经典.!! 1.把c++当成一门新的语言学习(和c没啥关系!真的.): 2.看<thinking in c++>,不要看<c++变成死相>: ...
- run()和star()区别
run()和star()区别 run()-->只是thread类的一个普通方法调用 star()-->用来启动线程,实现多线程运行
- 从一个例子讲解拷贝构造函数与return
#include "iostream" using namespace std; class Location { public: Location(, ) { X = xx; Y ...
- linux env
.Linux的变量种类 按变量的生存周期来划分,Linux变量可分为两类: 1.1 永久的:需要修改配置文件,变量永久生效. 1.2 临时的:使用export命令声明即可,变量在关闭shell时失效. ...
- SQL Server数据库性能优化(一)之 优化SQL 语句
最近工作上基本没什么需求(好吧 不是最近是好久了,所以随便看看基础的东西来填补自己的空白) 原文出自:http://www.blogjava.net/allen-zhe/archive/2010/07 ...