Allowance
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4659   Accepted: 1848

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.

Source

[Submit]   [Go Back]   [Status]   [Discuss]

POJ  3040-Allowance(贪心)_数组Home Page   POJ  3040-Allowance(贪心)_非零值_02Go Back  POJ  3040-Allowance(贪心)_非零值_03To top

题意:给你n个面值的金币,每个金币有固定数量,每周你都要交至少m块钱,问你最多能撑多少周

题解:首先很容易想到的是先用大的,对于大于m面额的金币,直接答案加上该面值的数量。

对于小于m面值的金币,首先取大的,达到临界值后我往小的取,直到达到m,但是此时会出现两种情况。

(1)小的不管怎么加都加不到,呢我肯定还加大的。

(2)加超了,呢我就从前往后遍历,一个个的减去小的使得达到临界值即可。

一开始写超时了,于是我加了个优化:对于当前满足题意的组合方式,我开一个数组标记每个面值的金币用了几张

然后让这些面值的金币减去标记的最小非零值,然后就过了。。。。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
struct node
{
	ll x,y;   
}a[30];   
ll b[30];   
bool comp(node a,node b)
{
	return a.x<b.x;
}
int main(void)
{
	ll n,m,i,j,ans=0;
	scanf("%lld%lld",&n,&m);
	for(i=1;i<=n;i++)
		scanf("%lld%lld",&a[i].x,&a[i].y);
	sort(a+1,a+n+1,comp);
	for(i=n;i>0;i--)
	{
		//printf("%lld\n",a[i].y);
		if(a[i].y==0)
			continue;
		while(a[i].y!=0)
		{
			if(a[i].x>=m)
				ans+=a[i].y,a[i].y=0;
			else
			{
				ll tmp=m;
				memset(b,0,sizeof(b));
				while(a[i].y>0 && a[i].x<=tmp)
					tmp-=a[i].x,a[i].y--,b[i]++;
				for(j=i-1;j>0;j--)
				{
					while(a[j].y>0 && a[j].x<=tmp)
						tmp-=a[j].x,a[j].y--,b[j]++;
				}
				if(tmp!=0)
				{
					for(j=1;j<=n;j++)
					{
						while(a[j].y>0 && tmp>0)
							tmp-=a[j].x,a[j].y--,b[j]++;
					}
				}
				if(tmp>0)
					break;
				else
				{
					ans++;
					for(j=1;j<=n;j++)
					{
						if(b[j]==0)
							continue;
						while(b[j]>0 && tmp+a[j].x<=0)
							tmp+=a[j].x,a[j].y++,b[j]--;
					}
					ll mins=1e18;
					for(j=1;j<=n;j++)
					{
						if(b[j]==0)
							continue;
						mins=min(mins,a[j].y/b[j]);
					}
					//printf("%lld\n",mins);
					for(j=1;j<=n;j++)
						a[j].y-=b[j]*mins;
					ans+=mins;
				}
			}
		}
	}
	printf("%lld\n",ans);
	return 0;
}