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

    The Fewest Coins
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 7311   Accepted: 2255

    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, ...., 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, 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.
     
    题意:你需要用尽量少的硬币个数来交易一个价值为T的物品,问最少需要多少硬币。其中你手中拥有一定的硬币,个数有限,而卖家的各种面值的硬币都有,且数量无限制。交易的硬币个数=你使用的硬币数+卖家找零所用的硬币数。
    思路:完全背包。把问题分成两个部分,即支付多少硬币给卖家和卖家用多少硬币找零。
    第一部分就是有数量限制的完全背包,其中背包的重量可以看成是你支付的硬币的面值,背包的价值当作是你支付的硬币个数即可。
    第二部分找零,是一个无数量限制的完全背包,背包重量与价值的对应转换方式同第一部分。
    AC代码:
    #define _CRT_SECURE_NO_DEPRECATE
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<cstring>
    #include<string>
    #include<cmath>
    using namespace std;
    const int INF = 0x3f3f3f3f;
    const int N_MAX = 100 + 20;
    const int V_MAX = 120 + 10;
    const int T_MAX = 10000 + 10;
    typedef long long ll;
    int n, t;
    int v[N_MAX], c[N_MAX];
    int dp_pay[T_MAX + V_MAX*V_MAX], dp_rec[T_MAX + V_MAX*V_MAX];
    
    void pack_pay(int W) {
        dp_pay[0] = 0;
        for (int i = 0; i < n; i++) {
            int num = c[i];
            for (int k = 1; num>0; k <<= 1) {
                int mul = min(k, num);
                for (int j = W; j >= v[i] * mul; j--) {
                    dp_pay[j] = min(dp_pay[j], dp_pay[j - v[i] * mul] + mul);
                }
                num -= mul;
            }
        }
    }
    
    void pack_rec(int W) {
        dp_rec[0] = 0;
        for (int i = 0; i < n; i++) {
            for (int j = v[i]; j <= W; j++) {
                dp_rec[j] = min(dp_rec[j], dp_rec[j - v[i]] + 1);
            }
        }
    }
    
    int main() {
        while (scanf("%d%d", &n, &t) != EOF) {
            memset(dp_pay, INF, sizeof(dp_pay));
            memset(dp_rec, INF, sizeof(dp_rec));
            for (int i = 0; i < n; i++)
                scanf("%d", v + i);
            for (int i = 0; i < n; i++)
                scanf("%d", c + i);
            int max_v = *max_element(v, v + n);
            pack_pay(t + max_v*max_v);
            pack_rec(max_v*max_v);
            int sum = INF;
            for (int i = 0; i <= max_v*max_v; i++) {
                sum = min(sum, dp_pay[t + i] + dp_rec[i]);
            }
            if (sum == INF)sum = -1;
            printf("%d
    ", sum);
        }
    
        return 0;
    }
  • 相关阅读:
    HDU 2865 Birthday Toy
    POJ 2888 Magic Bracelet
    BZOJ 3105 新Nim游戏
    BZOJ 2916 Monochromatic Triangles
    Countries
    Memory and Scores
    Paint on a Wall
    这是一道智障题
    可持久化数据结构
    数一的逆袭
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/9031940.html
Copyright © 2011-2022 走看看