POJ 1252 Euro Efficiency(完全背包变型)

原创 2016年08月29日 23:15:24
Euro Efficiency
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3950   Accepted: 1697

Description

On January 1st 2002, The Netherlands, and several other European countries abandoned their national currency in favour of the Euro. This changed the ease of paying, and not just internationally. 
A student buying a 68 guilder book before January 1st could pay for the book with one 50 guilder banknote and two 10 guilder banknotes, receiving two guilders in change. In short:50+10+10-1-1=68. Other ways of paying were: 50+25-5-1-1, or 100-25-5-1-1.Either way, there are always 5 units (banknotes or coins) involved in the payment process, and it 
could not be done with less than 5 units. 
Buying a 68 Euro book is easier these days: 50+20-2 = 68, so only 3 units are involved.This is no coincidence; in many other cases paying with euros is more efficient than paying with guilders. On average the Euro is more efficient. This has nothing to do, of course, with the value of the Euro, but with the units chosen. The units for guilders used to be: 1, 2.5, 5, 10, 25, 50,whereas the units for the Euro are: 1, 2, 5, 10, 20, 50. 
For this problem we restrict ourselves to amounts up to 100 cents. The Euro has coins with values 1, 2, 5, 10, 20, 50 eurocents. In paying an arbitrary amount in the range [1, 100] eurocents, on average 2.96 coins are involved, either as payment or as change. The Euro series is not optimal in this sense. With coins 1, 24, 34, 39, 46, 50 an amount of 68 cents can be paid using two coins.The average number of coins involved in paying an amount in the range [1, 100] is 2.52. 
Calculations with the latter series are more complex, however. That is, mental calculations.These calculations could easily be programmed in any mobile phone, which nearly everybody carries around nowadays. Preparing for the future, a committee of the European Central Bank is studying the efficiency of series of coins, to find the most efficient series for amounts up to 100 eurocents. They need your help. 
Write a program that, given a series of coins, calculates the average and maximum number of coins needed to pay any amount up to and including 100 cents. You may assume that both parties involved have sufficient numbers of any coin at their disposal. 

Input

The first line of the input contains the number of test cases. Each test case is described by 6 different positive integers on a single line: the values of the coins, in ascending order. The first number is always 1. The last number is less than 100. 

Output

For each test case the output is a single line containing first the average and then the maximum number of coins involved in paying an amount in the range [1, 100]. These values are separated by a space. As in the example, the average should always contain two digits behind the decimal point. The maximum is always an integer. 

Sample Input

3
1 2 5 10 20 50
1 24 34 39 46 50
1 2 3 7 19 72

Sample Output

2.96 5
2.52 3
2.80 4

Source

Northwestern Europe 2002


思路:

这个题光看题就看了好久好久(我太菜了...)题意大体就是 有6种货币,它们的面值分别为1,v2,v3,v4,v5,v6. 且它们面值最大值小于100. 最小值始终是1(就是那个v1). 现在的问题是 要你用尽量少的这6种货币构造1到100的所有面值的金钱. 问你构造这100个数平均需要多少个货币? 最多需要多少个货币?注意: 这里的构成不仅包含相加, 还能相减. 即假设货币面值为:要1 3 5 7 9 11 时, 你需要构造4. 那么你可以用1+3, 你也可以用5-1来构造, 你还可以用1+1+1+1来构造.

网上的说法都是什么二次完全背包正序逆序各来一遍...我的想法和他们不一样,因为虽然又可以通过加法得到钱和减法得到钱两种方式,这样虽然会增加问题的难度(这样也是为什么要正序逆序各来一遍的原因),但是可以换一种思路想,减钱实际上就是加上负的钱,所以,对于给定的6种基本货币,我们可以再定义6种它们的负的货币,这样一共12种货币,来进行完全背包操作。不过加货币和减货币的递推顺序不同, 加货币是从货币总值小到大递推, 而减货币是从货币总值大到小递推. 对于不同c[i]不同处理即可。一开始也是蜜汁哇,看了网上的说法都说把遍历的范围改到2000就会过了,好像是因为如果只遍历到100的话会漏掉很多种情况,而到2000就足够了。


代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define inf 1e9  
#define min(a,b)    ((a)<(b)?(a):(b))  
using namespace std;
int dp[2010];  

int main()
{
	//freopen("in.txt","r",stdin);
    int t;  
    scanf("%d", &t);  
    while (t--)  
    {  
        int c[13];    
        for (int i=1; i<=6; i++)
		{
			scanf("%d", &c[i]); 
			c[i+6]=-c[i];
		}  
        int max = 0, sum = 0;  
        for(int i=0;i<2001;i++)  //我一开始用memset inf,但是这样做不行...
            dp[i]=inf;   
        dp[0] = 0;  
          
		for (int i = 1; i<=12; i++)
		{
			 if(c[i]>0)  
            {  
                for(int j=c[i];j<2001;j++)  
                    dp[j]= min(dp[j], dp[j-c[i]]+1);  
            }  
            else if(c[i]<0)  
            {  
                int v = -c[i];  
                for(int j=2000;j>=v;j--)  
                    dp[j-v] = min(dp[j-v], dp[j]+1);  
            }  
		}
		   
  
        for (int i=1; i<=100; i++)  
        {  
            sum += dp[i];  
            if (dp[i] >= max)  
                max = dp[i];  
        }  
        printf("%.2lf %d\n", sum/100.0, max);  
    }  
    return 0;  		
}


还可以用bfs、两次完全背包来做。


版权声明:本博客为博主编程日记,转载请注明来源。

POJ 1252 Euro Efficiency【完全背包正负】

Euro Efficiency Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3649  ...
  • wyjwyl
  • wyjwyl
  • 2015年09月12日 16:46
  • 224

poj 1252 Euro Efficiency(多次完全背包)

Euro Efficiency Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1866  ...
  • fp_hzq
  • fp_hzq
  • 2011年10月03日 21:22
  • 1595

poj 1252 Euro Efficiency 正负完全背包

//由于输入的货币正负都可以,所以要分正负两次背包 //记的完全背包的时候不要放在一个for(i)里, //应该先正后负,因为是从0开始的,币值不能到负的, //但其实可以先出现负的,之后又加正的变为...

POJ 1252 Euro Efficiency(BFS or 完全背包)

POJ 1252 Euro Efficiency(BFS or 完全背包) http://poj.org/problem?id=1252 题意: 有6种货币,它们的面值分别为1,v2,v...

Euro Efficiency 1252 (正负完全背包 好题)

Euro Efficiency Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3552   Accepted: ...

poj1252Euro Efficiency

Euro Efficiency Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3321  ...

POJ 1252 Euro Efficiency

背包 或者 BFS 题意是说给你几种基本货币,组成 1~100 内所有的钱,用最少的基本货币使用量。 输出 使用量的概率,和最大的使用量。 可以BFS 也可以 背包。 不过记得...

poj 1252:Euro Efficiency

给予6种硬币值,求1~100块钱平均可以用多少个硬币得到与最多的需要多少硬币得到。可以加可以减。   网上看说是完全背包问题,不过做下来也没感觉是完全背包,反正按思路来看感觉不是。方法是先加上去,...

完全背包的变形POJ1252

话说今天做背包做到有点累了,题目是英文的……而且还很长,我看了好久(弱爆了)。 题目大概的意思就是,有六种硬币,之后,求用这六种硬币最小数目支付1到100美分的平均值,以及最小数目中的最大值。 很容易...

【POJ】1787 Charlie's Change(完全背包)

Charlie's Change
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 1252 Euro Efficiency(完全背包变型)
举报原因:
原因补充:

(最多只允许输入30个字)