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

    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 C1coins of value V1C2 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 (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, respectivelyV 1V 2, ..., VN coins ( V 1, ... VN
    Line 3: N space-separated integers, respectivelyC 1C 2, ..., 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.
     
    题目意思:给出钱币的方案数和总价值,然后给出每种钱币的价值与数量,而老板也是每种钱币都拥有,但是没有数量限制,购买东西的时候,价值超过给定价值的话,老板会找钱,要求最小的交流钱币的数量
    解题思路:多重背包和完全背包混合使用
     
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include<math.h>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 const int MAX = 40000;
     9 int N,M;
    10 int a[MAX],num[MAX],dp1[MAX],dp2[MAX],dp3[MAX];
    11 int main()
    12 {
    13     while(~scanf("%d %d",&N,&M))
    14     {
    15         for(int i=1;i<=N;i++)
    16             scanf("%d",&a[i]);
    17         for(int i =1;i<=N;i++)
    18             scanf("%d",&num[i]);
    19 
    20         for(int i=1;i<=MAX;i++)
    21             dp1[i]=dp2[i]=MAX*1000;
    22         dp1[0]=dp2[0]=0;
    23         for(int i=1;i<=N;i++)
    24         {
    25             memset(dp3,0,sizeof(dp3));
    26             for(int j=a[i];j<=MAX;j++)
    27             {
    28                 if(dp1[j]>dp1[j-a[i]]+1&&dp3[j-a[i]]<num[i])
    29                 {
    30                     dp1[j] = dp1[j-a[i]]+1;
    31                     dp3[j] =dp3[j-a[i]] +1;
    32                 }
    33                 dp2[j] = min(dp2[j],dp2[j-a[i]]+1);
    34             }
    35         }
    36 
    37         int minx=MAX*1000;
    38         for(int i = M;i<=MAX;i++)
    39         {
    40             minx = min(minx,dp1[i]+dp2[i-M]);
    41         }
    42         if(minx>=MAX*1000)
    43             printf("-1
    ");
    44         else
    45             printf("%d
    ",minx);
    46 
    47 
    48     }
    49 
    50     return 0;
    51 }



  • 相关阅读:
    POJ 1061
    hihocoder 1330
    HDU 1525
    UVALive 3938
    POJ 2528
    HDU 1754
    《ACM-ICPC程序设计系列 数论及其应用》例题个人答案记录
    URAL 1277
    HDU 3746
    HDU 2087
  • 原文地址:https://www.cnblogs.com/a2985812043/p/7375671.html
Copyright © 2011-2022 走看看