HDU5727(贪心)

分析:这个题可以反过来想。用最少的硬币把多出来的钱凑完。但是输入并不保证可以正好凑出来。所以必须考虑完不成的情况。因为10种面值里面只有50和500前面的面值不是他的因子(20和200)。所以得考虑是否把50和500都用完。因为50不能由20组成,500不能由200组成,所以每次搜索时要考虑全用或者留一个。从而保证结果最优!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
#define nl n<<1
#define nr (n<<1)1
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f;
const ll INFF=0x3f3f3f3f3f3f3f3f;
const double pi=acos(-1.0);
const double eps=1e-9;
int f[]={0,1,5,10,20,50,100,200,500,1000,2000};
int a[15];
int fun(int mo,int me)
{
    if(me==0)return 0;
    if(mo==0)return -1;
    int x1=INF,x2=INF;
    int h=min(a[mo],me/f[mo]);
    int tmp=fun(mo-1,me-f[mo]*h);
    if(tmp!=-1) x1=tmp+h;
    if(h>0)
    {
        tmp=fun(mo-1,me-f[mo]*(h-1));
        if(tmp!=-1)
            x2=tmp+h-1;
    }
    if(x1==INF&&x2==INF)return -1;
    return min(x1,x2);
}
int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        int p;scanf("%d",&p);
        int sum=0,tot=0;
        for(int i=1;i<=10;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i]*f[i];tot+=a[i];
        }
        int m=sum-p;
        if(m<0){printf("-1\n");continue;}
        int tmp=fun(10,m);
        if(tmp==-1) printf("-1\n");
        else printf("%d\n",tot-tmp);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值