站点图标 陌路寒暄

Exact Change

Exact Change

时间: 1ms        内存:128M

描述:

  • 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

提示:

参考答案(内存最优[9344]):

#include <stdio.h>
#include <string.h>

int coins[1100000];
unsigned dp[1100000];

int comp(const int* a, const int *b) {
    return (*a)-(*b);
}
main() {
    int nc;
    int i,j;
    int CASES;
    int price;
    scanf("%d", &CASES);
    while(CASES--) {
        memset(dp,-1,sizeof(dp));
        dp[0]=0;
        scanf("%d", &price);
        scanf("%d", &nc);
        for(i=0;i<nc;i++) {
            scanf("%d", coins+i);
        }
        for(i=0;i<nc;i++) {
            for(j=price-1;j>=0;j--) {
                if(dp[j]==-1) continue;
                if(dp[j+coins[i]] > dp[j]+1) dp[j+coins[i]] = dp[j]+1;
            }
        }
        for(i=price;dp[i]==-1;i++);
        printf("%d %d\n", i, dp[i]);
    }
}

参考答案(时间最优[8]):

#include <stdio.h>
#include <string.h>

int coins[1100000];
unsigned dp[1100000];

int comp(const int* a, const int *b) {
    return (*a)-(*b);
}
main() {
    int nc;
    int i,j;
    int CASES;
    int price;
    scanf("%d", &CASES);
    while(CASES--) {
        memset(dp,-1,sizeof(dp));
        dp[0]=0;
        scanf("%d", &price);
        scanf("%d", &nc);
        for(i=0;i<nc;i++) {
            scanf("%d", coins+i);
        }
        for(i=0;i<nc;i++) {
            for(j=price-1;j>=0;j--) {
                if(dp[j]==-1) continue;
                if(dp[j+coins[i]] > dp[j]+1) dp[j+coins[i]] = dp[j]+1;
            }
        }
        for(i=price;dp[i]==-1;i++);
        printf("%d %d\n", i, dp[i]);
    }
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

退出移动版