zoukankan      html  css  js  c++  java
  • poj3260 The Fewest Coins

    Description

    Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he always pays for his goods in such a way that the smallest number of coins changes hands, i.e., the number of coins he uses to pay plus the number of coins he receives in change is minimized. Help him to determine what this minimum number is.

    FJ wants to buy T (1 ≤ T ≤ 10,000) cents of supplies. The currency system has N (1 ≤ N ≤ 100) different coins, with values V1V2, ..., VN (1 ≤ Vi ≤ 120). Farmer John is carrying C1 coins of value V1C2 coins of value V2, ...., andCN 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 (although Farmer John must be sure to pay in a way that makes it possible to make the correct change).

    Input

    Line 1: Two space-separated integers: N and T
    Line 2: N space-separated integers, respectively V1V2, ..., VN coins (V1, ...VN
    Line 3: N space-separated integers, respectively C1C2, ..., CN

    Output

    Line 1: A line containing a single integer, the minimum number of coins involved in a payment and change-making. If it is impossible for Farmer John to pay and receive exact change, output -1.

    Sample Input

    3 70
    5 25 50
    5 2 1

    Sample Output

    3

    Hint

    Farmer John pays 75 cents using a 50 cents and a 25 cents coin, and receives a 5 cents coin in change, for a total of 3 coins used in the transaction.

    这题思路是一次枚举钱数,从要买物品的价格到最大值(即上界),这里最大值为24400(查百度的,用鸽笼原理),然后用num1[m]表示买m元买家最少要用的硬币数(可以用多重背包),num2[m]表示找钱m元最少要用的硬币数(可以用完全背包,因为数量无限),然后设一个值ans,用min(ans,num1[i]+num2[i-jiage])求出最小的硬币数。 

    #include<stdio.h>
    #include<string.h>
    #define inf 88888888
    int min(int a,int b){
    	return a<b?a:b;
    }
    int num1[25500],num2[25500],v[105],num[105],w[105];
    int main()
    {
    	int n,i,j,jiage,ans,k,sum;
    	int m=24450;
    	while(scanf("%d%d",&n,&jiage)!=EOF)
    	{
    		for(i=1;i<=n;i++){
    			scanf("%d",&w[i]);
    			v[i]=1;
    		}
    		for(i=1;i<=n;i++){
    			scanf("%d",&num[i]);
    		}
    		for(j=1;j<=m;j++){
    			num1[j]=num2[j]=inf;
    		}
    		num1[0]=num2[0]=0;
    		
    		for(i=1;i<=n;i++){
    			for(j=w[i];j<=m;j++){
    				if(num2[j-w[i]]!=inf){
    					num2[j]=min(num2[j],num2[j-w[i]]+v[i]);
    				}
    			}
    			
    			ans=num[i]*w[i];
    			if(ans>=m){
    				for(j=w[i];j<=m;j++){
    					if(num1[j-w[i]]!=inf){
    						num1[j]=min(num1[j],num1[j-w[i]]+v[i]);
    					}
    				}
    			}
    			else{
    				k=1;sum=0;
    				while(sum+k<num[i]){
    					sum+=k;
    					for(j=m;j>=k*w[i];j--){
    						if(num1[j-k*w[i]]!=inf){
    							num1[j]=min(num1[j],num1[j-k*w[i]]+k*v[i]);
    						}
    					}
    					k=k*2;
    				}
    				k=num[i]-sum;
    				for(j=m;j>=k*w[i];j--){
    						if(num1[j-k*w[i]]!=inf){
    							num1[j]=min(num1[j],num1[j-k*w[i]]+k*v[i]);
    						}
    				}
    			}
    		}
    		ans=inf;
    		for(i=jiage;i<=m;i++){
    			if(num1[i]==inf || num2[i-jiage]==inf)continue;
    			if(ans>num1[i]+num2[i-jiage]){
    				ans=num1[i]+num2[i-jiage];
    			}
    		}
    		if(ans!=inf)
    		printf("%d
    ",ans);
    		else printf("-1
    ");
    	}
    	return 0;
    }


  • 相关阅读:
    C# 下 WebService 初探: 构建Web Service 服务及 WinForm和浏览器 httpget调用
    Action<T> 泛型委托 在跨线程访问控件委托中的应用
    C# 网络连接中异常断线的处理:ReceiveTimeout, SendTimeout 及 KeepAliveValues(设置心跳)
    DOS下的批处理文件(老东西了)
    typedef struct和struct定义结构体的区别
    多线程idhttp下载文件源代码
    JQUERY获取DOM对象
    学习笔记:delphi实现网络通信之select模型
    学习笔记之WSAAsyncSelect模式
    delphi 函数指针 方法指针
  • 原文地址:https://www.cnblogs.com/herumw/p/9464727.html
Copyright © 2011-2022 走看看