Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %lld & %llu |
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.
* 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.
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
无奈---我求了所有的钱数----但是用的 int 然后bug 找了一下午---最后才想到这个问题上--无语--
思路就是让每周给的钱尽可能的少---(贪婪法---尽可能的---)
所以最后就补上一个最小的---
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
int n,c;
long long s,ss;
struct node{
LL v,ge;
}pp[30];
bool cmp(node xx,node yy)
{
return xx.v<yy.v;
}
void zhao(int cc)
{
int lp=0;
int ge[30]={0};
for (int i=n-1;i>=0;i--)//先拿大的靠近c
{
ge[i]=min(pp[i].ge,cc/pp[i].v);
cc-=ge[i]*pp[i].v;
}
if (cc)//不够时找一个还剩余的中最小的补----(开始用求余,所以最小的一个一定可以补上)
{
for (int i=0;i<n;i++)
if (pp[i].ge>ge[i])
{
ge[i]++;
cc-=pp[i].v;
break;
}
}
LL ci=10000000;
for (int i=0;i<n;i++)
if (ge[i])
ci=min(ci,pp[i].ge/ge[i]);
ss+=ci;s=s-(c-cc)*ci;
for (int i=0;i<n;i++)
pp[i].ge-=ge[i]*ci;
}
int main()
{
while (~scanf("%d%d",&n,&c))
{
for (int i=0;i<n;i++)
scanf("%lld%lld",&pp[i].v,&pp[i].ge);
sort(pp,pp+n,cmp);
ss=0;
while (n&&pp[n-1].v>=c)
{
n--;
ss+=pp[n].ge;
}
if (n==0)
{
printf("%lld\n",ss);
continue;
}
s=0;
for (int i=0;i<n;i++)
s+=pp[i].ge*pp[i].v;//会超出int
while (s>=c)
zhao(c);
printf("%lld\n",ss);
}
return 0;
}