Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5599 | Accepted: 1688 |
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 V1, V2, ..., VN (1 ≤ Vi ≤ 120). Farmer John is carrying C1coins of value V1, C2 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 2: N space-separated integers, respectively V 1, V 2, ..., VN coins ( V 1, ... VN)
Line 3: N space-separated integers, respectively C 1, C 2, ..., CN
Output
Sample Input
3 70 5 25 50 5 2 1
Sample Output
3
Hint
Source
无论是 买家或者是卖家 他们都是背包中的物品。 但是要求是 买家的V >= 商品价格。 其他没了。就是这么简单。
还有一个最大值的估计。 如果大于了V+max*max 那么卖家 的商品价格之和为V + max*max - V = max*max
买家的商品价格之和为V + max* max.
大于max 那么我们可以知道 sb1 b2 b3 .. bmax bmax+1 一共 max+1个数字
那么对于队列sum(1) sum(2) 存在2个数字mod max相同, 那么我们可以知道sum(j) mod max = sum(i) mod max 所以 b[i+1,...j] 是max的倍数, 那么卖家可以用一张或者多张maxv来换b[i+1,...j]
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
int m,n;
const int maxn = 110;
int a[maxn];
int c[maxn];
int MAXV;
int TOT;
const int maxt = 10000 + 120 * 120 + 1;
int f1[maxt];
int f2[maxt];
int main()
{
while(scanf("%d%d",&m,&n) != EOF)
{
MAXV = -1;
for(int i=0;i<m;i++)
{
scanf("%d",&a[i]);
MAXV = max(MAXV,a[i]);
}
MAXV *= MAXV;
TOT = MAXV + n;
for(int i=0;i<m;i++)
{
scanf("%d",&c[i]);
}
memset(f1,0x3f3f3f3f,sizeof(f1));
f1[0] = 0;
for(int i=0;i<m;i++)
{
for(int j=a[i];j<=MAXV;j++)
{
f1[j] = min(f1[j],f1[j-a[i]] + 1);
}
}
memset(f2,0x3f3f3f3f,sizeof(f2));
f2[0] = 0;
for(int i=0;i<m;i++)
{
int sum = 0;
int k = 1;
while(sum < c[i])
{
for(int j=TOT;j>=k*a[i];j--)
{
f2[j] = min(f2[j], f2[j-a[i]*k] + k);
}
sum += k;
if(sum + 2*k > c[i])
{
k = c[i] - sum;
}
else
{
k *= 2;
}
}
}
int _min = 0x3f3f3f3f;
for(int i=n;i<=TOT;i++)
{
_min = min(_min,f2[i] + f1[i-n]);
}
if(_min == 0x3f3f3f3f)
{
printf("-1\n");
}
else
{
printf("%d\n",_min);
}
}
return 0;
}