1685: [Usaco2005 Oct]Allowance 津贴

1685: [Usaco2005 Oct]Allowance 津贴

Time Limit: 5 Sec   Memory Limit: 64 MB
Submit: 193   Solved: 141
[ Submit][ Status][ Discuss]

Description

As a reward for record milk production, Farmer John has decided to start paying Bessie the cow a small weekly allowance. FJ has a set of coins in N (1 <= N <= 20) different denominations, where each denomination of coin evenly divides the next-larger denomination (e.g., 1 cent coins, 5 cent coins, 10 cent coins, and 50 cent coins). Using the given set of coins, he would like to pay Bessie at least some given amount of money C (1 <= C <= 100,000,000) every week. Please help him compute the maximum number of weeks he can pay Bessie.

作为对勤勤恳恳工作的贝茜的奖励,约翰已经决定开始支付贝茜一个小的每周津贴.   约翰有n(1≤N≤20)种币值的硬币,面值小的硬币总能整除面值较大的硬币.比如说,币值有如下几种:1美分,5美分,10美分,50美分…..
    利用给定的这些硬币,他将要每周付给贝茜一定金额的津贴C(1≤C≤10^8).
    请帮他计算出他最多能给贝茜发几周的津贴.

Input

    第1行:2个用空格隔开的整数n和C.
    第2到n+1行:每行两个整数表示一种币值的硬币.第一个整数V(I≤y≤10^8),表示币值.
第二个整数B(1≤B≤10^6),表示约翰拥有的这种硬币的个数.

Output

    一个整数,表示约翰付给贝茜津贴得最多的周数.

Sample Input

3 6
10 1
1 1 00
5 1 20

Sample Output

111
样例说明
约翰想要每周付给贝茜6美分.他有1个10美分的硬币、100个1美分的硬币、120个5美分的硬币.约翰可以第一周付给贝茜一个10美分的硬币,接着的10周每周付给贝茜2个5芙分硬币,接下来的100周每周付给贝茜一个1美分的硬币和1个5美分的硬币.共计111周.

HINT

Source

 

对这题显然每周花的冤枉钱越少越好。。

因为前面的硬币总能整除后面的,所以排个序从大到小去找能找到最优方案。。。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

const int maxn = 30;

struct C{
	int v,b;
	bool operator < (const C &B) const {
		return v < B.v;
	}
}coin[maxn];

int n,i,j,C,h[maxn];
long long ans = 0;

int main()
{
	#ifdef YZY
		freopen("yzy.txt","r",stdin);
	#endif
	
	cin >> n >> C;
	for (i = 1; i <= n; i++) scanf("%d%d",&coin[i].v,&coin[i].b);
	sort (coin + 1,coin + n + 1);
	while (1)
	{
		int rest = C;
		memset(h,0,sizeof(h));
		for (i = n; i > 0; i--)
		{
			int temp = min(coin[i].b,rest / coin[i].v);
			rest -= temp * coin[i].v;
			h[i] = temp;
		}
		if (rest > 0)
		  for (i = 1; i <= n; i++)
		  {
		  	if (rest <= 0) break;
		  	if (coin[i].b > h[i])
		  	{
		  		++h[i];
		  		rest -= coin[i].v;
		  	}
		  }
		if (rest <= 0)
		{
			int tot = 1E9;
			for (i = 1; i <= n; i++)
			  if (h[i] > 0) tot = min(tot,coin[i].b / h[i]);
			ans += 1LL*tot;
			for (i = 1; i <= n; i++)
			  if (h[i] > 0) coin[i].b -= tot * h[i];
		}
		else break;
	}
	cout << ans;
	return 0;
}


 

你可能感兴趣的:(1685: [Usaco2005 Oct]Allowance 津贴)