Too Rich UVALive - 7183 (greedy)

P request is to take the money, bringing the total number of coins to obtain maximum
reverse thinking is to strike a sum-p money, bringing the total number of coins taken at least
so greedy it will be easy
from the start we can observe the biggest addition to 20 50 200 500 remaining outside and adjacent multiple relationship between the two is
so in addition to these two particular it
and because 20 is a multiple of 50 + 50 so the time taken to consider taking less 50 a 50 case (with 200,500)
, such as when there are 3 50 50 + 50 is a multiple of 20 can be expressed but if it is not, then 50 * 3 20 expressed the need to have to run it

#include<bits/stdc++.h>

#define inf 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=a;i<=b;i++)

#define int long long

using namespace std;

int a[15]={0,1,5,10,20,50,100,200,500,1000,2000};
int mub[15],ans;

void dfs(int sum,int t,int now)
{
    if(sum==0)
    {
        ans=min(t,ans);
        return ;
    }
    if(now==0) return ;
    int m=sum/a[now];
    m=min(m,mub[now]);
    dfs(sum-m*a[now],t+m,now-1);
    if(m)
    {
        m--;
        dfs(sum-m*a[now],t+m,now-1);
    }

}
#undef int
int main()
{
#define int long long
    int t;
    cin>>t;
    while(t--)
    {
        ans=inf;
        int n,sum=0,all=0;
        cin>>n;
        rep(i,1,10) cin>>mub[i],sum+=a[i]*mub[i],all+=mub[i];
        sum=sum-n;
        if(sum>0)
            dfs(sum,0,10);
        cout<<(ans==inf?-1:all-ans)<<endl;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/minun/p/11504420.html