淘先锋技术网

首页 1 2 3 4 5 6 7
L - Allowance
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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 ompute the maximum number of weeks he can pay Bessie.

Input

* Line 1: Two space-separated integers: N and C 

* Lines 2..N+1: Each line corresponds to a denomination of coin and contains two integers: the value V (1 <= V <= 100,000,000) of the denomination, and the number of coins B (1 <= B <= 1,000,000) of this denomation in Farmer John's possession.

Output

* Line 1: A single integer that is the number of weeks Farmer John can pay Bessie at least C allowance

Sample Input

3 6
10 1
1 100
5 120

Sample Output

111

Hint

INPUT DETAILS: 
FJ would like to pay Bessie 6 cents per week. He has 100 1-cent coins,120 5-cent coins, and 1 10-cent coin. 

OUTPUT DETAILS: 
FJ can overpay Bessie with the one 10-cent coin for 1 week, then pay Bessie two 5-cent coins for 10 weeks and then pay Bessie one 1-cent coin and one 5-cent coin for 100 weeks.

题意:

从大到小排序,只要不超额就能放多少就放多少,最后再从小的开始找一个放进去能超额的。

正确性证明,因为大的是小的倍数,所以大的放进去不超额一定要放进去,因为小的不管怎么取,再超过c之前一定会凑成这个大的面额,那么用大的代替一定更优。

第一步做完之后,那么现在一定要再放进去一个硬币,那么选择最小的并且能大于c的也一定是最优的。



AC代码:

/*
这个贪心比较坑的的地方是我写的程序
原来是在没有满足到>C是都加了一次值
了,所以就Wa了。

样例:
4 7
9 1
6 1
1 0
3 2

ans:
2
*/



#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<queue>
#include<vector>
#include<set>
using namespace std;
const int T=55000;
#define inf 0x3f3f3f3fL
typedef long long ll;

struct node
{
	int v,w;
}a[50];

bool cmp(const node& a,const node& b)
{
	return a.v>b.v;
}

int main()
{
#ifdef zsc
	freopen("input.txt","r",stdin);
#endif

	int n,m,i,j;
	while(~scanf("%d%d",&n,&m))
	{
		int num=0,cnt=0;
		int k = 0;
		memset(a,0,sizeof(a));
		for(i=0;i<n;++i){
			scanf("%d%d",&a[i].v,&a[i].w);
			//排除直接可以支付的
			if(a[i].v>=m)
				k+=a[i].w,i--,n--;
			else
				cnt += a[i].w;
		}
		sort(a,a+n,cmp);
		
		while(cnt>0)
		{
			num = m;
			//从大到小给钱
			for(i=0;i<n;++i){
				if(!a[i].w)continue;
				if(num>0){
					int tmp = min(num/a[i].v,a[i].w);
					a[i].w -= tmp;
					cnt -= tmp;
					num -= tmp*a[i].v;
				}
				if(num<=0)break;
			}
			//从小到大给钱
			if(num>0){
				for(i=n-1;i>=0;--i){
					if(a[i].w){
						while(num>0&&a[i].w)
							num-=a[i].v,a[i].w--,cnt--;
						if(num<=0)break;
					}
				}
			}
			if(num<=0)k++;//改了这里就ac了
		}
		printf("%d\n",k);
	}
	return 0;
}