必威体育Betway必威体育官网
当前位置:首页 > IT技术

Allowance(贪心+STL)

时间:2019-08-11 20:13:13来源:IT技术作者:seo实验室小编阅读:82次「手机版」
 

allowance

Problem:Allowance

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 pides 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

Note:

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.

Language:C++

#include <iOStream>
#include <algorithm>

using namespace std;

struct coins
{
  int V;
  int B;
}coin[25];

bool cmp(coins a,coins b)
{
  return a.V>b.V;
}

int main()
{
  int N,C;
  while(cin>>N>>C)
  {
    int ans=0;
    for(int i=0;i<N;i++)
    {
      cin>>coin[i].V>>coin[i].B;
      if(coin[i].V>=C)
      {
        ans+=coin[i].B;
        coin[i].B=0;
      }
    }

    sort(coin,coin+N,cmp);

    out:
    int sum=0;
    for(int i=0;i<N;i++)
    {
      while(coin[i].B)
      {
        sum+=coin[i].V;
        coin[i].B--;

        if(sum==C)
        {
          ans++;
          goto out;
        }

        else if(sum>C)
        {
          sum-=coin[i].V;
          coin[i].B++;
          break;
        }
      }
    }

    for(int i=N-1;i>=0;i--)
    {
      while(coin[i].B)
      {
        sum+=coin[i].V;
        coin[i].B--;
        if(sum>=C)
        {
          ans++;
          goto out;
        }
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

相关阅读

分享到:

栏目导航

推荐阅读

热门阅读