zoukankan      html  css  js  c++  java
  • Exact Change(01背包)

    描述

    • Seller: That will be fourteen dollars.
    • Buyer: Here's a twenty.
    • Seller: Sorry, I don't have any change.
    • Buyer: OK, here's a ten and a five. Keep the change.
    When travelling to remote locations, it is often helpful to bring cash, in case you want to buy something from someone who does not accept credit or debit cards. It is also helpful to bring a variety of denominations in case the seller does not have change. Even so, you may not have the exact amount, and will have to pay a little bit more than full price. The same problem can arise even in urban locations, for example with vending machines that do not return change.

    Of course, you would like to minimize the amount you pay (though you must pay at least as much as the value of the item). Moreover, while paying the minimum amount, you would like to minimize the number of coins or bills that you pay out.

    输入

    The first line of input contains one integer specifying the number of test cases to follow. Each test case begins with a line containing an integer, the price in cents of the item you would like to buy. The price will not exceed 10 000 cents (i.e., $100). The following line contains a single integer n, the number of bills and coins that you have. The number n is at most 100. The following n lines each contain one integer, the value in cents of each bill or coin that you have. Note that the denominations can be any number of cents; they are not limited to the values of coins and bills that we usually use in Canada. However, no bill or coin will have a value greater than 10 000 cents ($100). The total value of your bills and coins will always be equal to or greater than the price of the item.

    输出

    For each test case, output a single line containing two integers: the total amount paid (in cents), and the total number of coins and bills used.

    样例输入

    1
    1400
    3
    500
    1000
    2000

    样例输出

     1500 2

    题目大意:

    已知一件物品的价格,和n个有价值的硬币,现在求这些硬币能凑成的不小于这件物品的价格的最小价值。

    01背包。

    #include <bits/stdc++.h>
    using namespace std;
    const int INF=0x3f3f3f3f;
    int dp[10005],a[105];
    int main()
    {
        ios::sync_with_stdio(false);
        int T;
        cin>>T;
        while(T--)
        {
            memset(dp,INF,sizeof dp);
            dp[0]=0;
            int k,n;
            cin>>k>>n;
            for(int i=0;i<n;i++)
                cin>>a[i];
            for(int i=0;i<n;i++)
                for(int j=10004;j>=a[i];j--)///最大的可能值开始
                    dp[j]=min(dp[j],dp[j-a[i]]+1);
            int pos;
            for(int i=k;i<10005;i++)
                if(dp[i]!=INF)
                    {pos=i;break;}
            cout<<pos<<' '<<dp[pos]<<'
    ';
        }
        return 0;
    }
  • 相关阅读:
    动态显隐jgGrid的列,改变列名
    从数据库获取bit类型判断的时候要注意大小写
    jqGrid自定义列的用法
    ERP中Ajax的使用
    给DataGrid或Repeater加载树状结构
    jqGrid显示树形结构
    使用json异步获取数据提交表单
    封装jQuery图表插件
    java 多线程 day10 获取线程的返回值 CallableAndFuture
    java 多线程 day09 线程池
  • 原文地址:https://www.cnblogs.com/zdragon1104/p/9189149.html
Copyright © 2011-2022 走看看