Rikka with Coin(10,20,50,100组成给定任意数的最少使用量)

Rikka hates coins, and she used to never carry any coins with her. These days, Rikka is doing her summer internship abroad. Without mobile payment, Rikka has to face strange prices of commodities, and as a result of always using paper currency, she has to face mountainous coins on here table. 

In the local currency system, there are 44 kinds of coins: 1010 cents, 2020 cents, 5050cents and 11 dollar. Up to now, Rikka has gained at least 1010010100 coins for each kind. 

Now, Rikka is going to have dinner in the canteen, and she decides to pay the bill only with coins. There are nn different combos in the canteen and the price of the iith is wiwi cents. Rikka would like to choose one combo as dinner but she has not decided to choose which one yet. Therefore, she wants to take some coins so that whichever she chooses, she can always pay the bill without receiving any change. 

Since Rikka hates coins, she wants to carry as few coins as possible with her. As it is generally known that Rikka is not good at math, she wants you to help her make the decision.

Input

The first line of the input contains a single integer T(1≤T≤500)T(1≤T≤500), the number of test cases. 

For each test case, the first line contains a single integer n(1≤n≤100)n(1≤n≤100), the number of combos sold in the canteen. 

The second line contains nn positive integers w1,…,wn(1≤wi≤109)w1,…,wn(1≤wi≤109), which represents the prices.

Output

For each test case, output a single line with a single integer which represents the minimum number of coins. If there is no valid solution, output −1−1. 

Hint

In the first test case, one optimal solution is to bring one coin of 1010 cents and two coins of 2020 cents. 

In the second test case, one optimal solution is to bring 55 coins of one dollar.

Sample Input

3
5
10 20 30 40 50
5
100 200 300 400 500
1
1

Sample Output

3
5
-1

2019杭电多校第九场第6题

题意:餐厅有n种饭,每种饭的价格为w[i],现在有10,20,50,100各无限张(1e100),问怎样组合才能满足所有的饭,且用的钞票数最少,不能满足输出 -1.

思路:首先,既然是10,20,50,100.那么只要有一种饭的价格不是10的倍数,那么肯定是要输出 -1的。

           同样,既然是10,20,50,100,那么我们都对其除10,就变成了1,2,5,10

           我们的目标是要满足所有饭的最少钞票的张数,那么优先级当然是10>5>2>1

           再来考虑答案中的情况:

                  1最多有几张呢?      答案肯定是1张了,毕竟>=2 就可以用面值为2的钞票替换了

                   2最多有几张呢?      答案是4张   ???  6=5+1=2+2+2(只有2没有5,1的时候)   8=2+2+2+2  

                   5最多有几张呢?      答案肯定是1张了,毕竟>=2 就可以用面值为10的钞票替换了

                  10最多有多少张呢?   答案是:w / 10 - ( 上面的1,2,5 能否凑出  10 + w%10)

          好了,此题已经解决了,那就是暴力枚举1,2,5的数量,用多重背包计算1,2,5能都组成(10 + w%10),如果可以的话,那么10元的张数为 w/10 - 1 ,否则我们要看是否能组成 w%10?如果可以的话为 w/10 ,不行的话就代表这组1,2,5的组合不对,继续枚举就好了,最后取min(a1+a2+a5+a10)

代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll inf=9999999999LL;
int w[110],dp[30],a[5]={1,2,5},m[5];
int solve(int x){
    memset(dp,-1,sizeof(dp));
    dp[0]=0;
    for(int i=0;i<3;i++){
        for(int j=0;j<15;j++){
            if(dp[j]>=0)dp[j]=m[i];
            else if(j<a[i]||dp[j-a[i]]<=0)dp[j]=-1;
            else dp[j]=dp[j-a[i]]-1;
        }
    }
    int tmp=x/10;
    int r=x%10;
    if(tmp>=1&&dp[r+10]>=0)return 1;
    if(dp[r]>=0)return 0;
    return -1;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        int flag=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&w[i]);
            if(w[i]%10!=0)flag=1;
            w[i]/=10;
        }
        if(flag){
            printf("-1\n");
        }
        else {
            ll minn=inf;
            sort(w+1,w+n+1);
            int p=1;
            for(int i=2;i<=n;i++){
                if(w[i]!=w[i-1]){
                    w[++p]=w[i];
                }
            }
            n=p;
            for(int a=0;a<=1;a++){
                for(int b=0;b<=4;b++){
                    for(int c=0;c<=1;c++){
                        int Flag=0;
                        int d=0;
                        for(int i=1;i<=n;i++){
                            int x=w[i]/10;
                            m[0]=a,m[1]=b,m[2]=c;
                            int mark=solve(w[i]);
                            if(mark==-1){
                                Flag=1;
                                break;
                            }
                            x-=mark;
                            d=max(d,x);
                        }
                        if(!Flag){
                            ll ans=(ll)a+b+c+d;
                            minn=min(minn,ans);
                        }
                    }
                }
            }
            if(minn==inf){
                printf("-1\n");
            }
            else {
                printf("%lld\n",minn);
            }
        }
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值