hdu3591The trouble of Xiaoqian【多重背包】

Problem Description
In the country of ALPC , Xiaoqian is a very famous mathematician. She is immersed in calculate, and she want to use the minimum number of coins in every shopping. (The numbers of the shopping include the coins she gave the store and the store backed to her.)
And now , Xiaoqian wants to buy T (1 ≤ T ≤ 10,000) cents of supplies. The currency system has N (1 ≤ N ≤ 100) different coins, with values V1, V2, ..., VN (1 ≤ Vi ≤ 120). Xiaoqian is carrying C1 coins of value V1, C2 coins of value V2, ...., and CN coins of value VN (0 ≤ Ci ≤ 10,000). The shopkeeper has an unlimited supply of all the coins, and always makes change in the most efficient manner .But Xiaoqian is a low-pitched girl , she wouldn’t like giving out more than 20000 once.
 

Input
There are several test cases in the input.
Line 1: Two space-separated integers: N and T.
Line 2: N space-separated integers, respectively V1, V2, ..., VN coins (V1, ...VN)
Line 3: N space-separated integers, respectively C1, C2, ..., CN
The end of the input is a double 0.
 

Output
Output one line for each test case like this ”Case X: Y” : X presents the Xth test case and Y presents the minimum number of coins . If it is impossible to pay and receive exact change, output -1.
 

Sample Input
3 70 5 25 50 5 2 1 0 0
 

Sample Output
Case 1: 3
 

这个题应该是多重背包要刷的第一个题,前几天看他有点难刷了前几个水题,今天看他恍然大悟,明晃晃的多重背包+完全背包 找钱用完全背包 从t到20000扫一遍找最小值呗

然而事情远没有那么顺利==第一次交WA发现中间变量忘记注释,第二次WA发现自己没输出格式Case i……后来是发现k倍之后就不再是+1而是+k了,再后来发现根本用不着四个数组==之前好像也是找钱之类的场景,要求输出正好的解,只是初始化的时候做了处理,并不用再设两个数组,典型的熊瞎子掰苞米……

/***********
hdu3519
2015.11.3
46MS 1872K 2297 B
***********/
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 20000
int dp1[maxn+6],value[105],num[105],t,n,dp2[maxn+6];//钱币个数
int f1[maxn+6],f2[maxn+6];//钱数
void zeropack(int cost,int value,int k)
{
    for(int i=maxn;i>=cost;i--)
    {
        if(dp1[i-cost]!=-1&&(dp1[i-cost]+k<dp1[i]||dp1[i]==-1))
        dp1[i]=dp1[i-cost]+k;
    }
   // cout<<"01"<<endl;
}
void completepack(int cost,int value)
{
    for(int i=cost;i<=maxn;i++)
    {
        if(dp1[i-cost]!=-1&&(dp1[i-cost]+1<dp1[i]||dp1[i]==-1))
        dp1[i]=dp1[i-cost]+1;
       // f1[i]=max(f1[i],f1[i-cost]+value);
    }
    //cout<<"complete"<<endl;
}
void multipack(int cost,int value,int num)
{
    if(num*cost>=maxn)
    {
        completepack(cost,value);
        return;
    }
    int k=1;
    while(k<num)
    {
        zeropack(k*cost,k*value,k);
        num-=k;
        k*=2;
    }
    zeropack(num*cost,num*value,k);
}
int main()
{
   // freopen("cin.txt","r",stdin);
    int cnt=1;
    while(~scanf("%d%d",&n,&t))
    {
        if(t==0&&n==0) break;
        for(int i=1;i<=n;i++) scanf("%d",&value[i]);
        for(int i=1;i<=n;i++) scanf("%d",&num[i]);
        memset(dp2,-1,sizeof(dp2));
        memset(dp1,-1,sizeof(dp1));
        memset(f1,0,sizeof(f1));
        memset(f2,0,sizeof(f2));
        dp2[0]=0;dp1[0]=0;
        for(int i=1;i<=n;i++)
            for(int j=value[i];j<=maxn;j++)
            {
                if(dp2[j-value[i]]!=-1&&(dp2[j]<dp2[j-value[i]]+1||dp2[j]==-1))
                dp2[j]=dp2[j-value[i]]+1;
               // f2[j]=max(f2[j],f2[j-value[i]]+value[i]);
            }
        for(int i=1;i<=n;i++)
        {
            multipack(value[i],value[i],num[i]);
        }
       // for(int i=70;i<=80;i++) printf("f1=%d f2=%d dp1=%d dp2=%d\n",f1[i],f2[i],dp1[i],dp2[i]);
        int minn=0x3f3f3f3f;
        for(int i=t;i<=maxn;i++)
        {
            //if(f1[i]!=i||f2[i-t]!=i-t) continue;
            if(dp1[i]!=-1&&dp2[i-t]!=-1)
            minn=min(minn,dp1[i]+dp2[i-t]);
        }
        if(minn!=0x3f3f3f3f)
        printf("Case %d: %d\n",cnt++,minn);
        else printf("Case %d: -1\n",cnt++);
    }
    return 0;
}


全部评论

相关推荐

老粉都知道小猪猪我很久没更新了,因为秋招非常非常不顺利,emo了三个月了,接下来说一下我的情况吧本人是双非本&nbsp;专业是完全不着计算机边的非科班,比较有优势的是有两段大厂实习,美团和字节。秋招面了50+场泡池子泡死的:滴滴&nbsp;快手&nbsp;去哪儿&nbsp;小鹏汽车&nbsp;不知名的一两个小厂其中字节13场&nbsp;两次3面挂&nbsp;两次2面挂&nbsp;一次一面挂其中有2场面试题没写出来,其他的都是全a,但该挂还是挂,第三次三面才面进去字节,秋招加暑期总共面了22次字节,在字节的面评可以出成书了快手面了8场,2次实习的,通过了但没去,一次2面挂&nbsp;最后一次到录用评估&nbsp;至今无消息滴滴三面完&nbsp;没几天挂了&nbsp;所有技术面找不出2个问题是我回答不上来的,三面还来说我去过字节,应该不会考虑滴滴吧,直接给我干傻了去哪儿一天速通&nbsp;至今无消息小鹏汽车hr&nbsp;至今无消息美团2面挂&nbsp;然后不捞我了,三个志愿全部结束,估计被卡学历了虾皮二面挂&nbsp;这个是我菜,面试官太牛逼了拼多多二面挂&nbsp;3道题也全写了&nbsp;也没问题是回答不出来的&nbsp;泡一周后挂腾讯面了5次&nbsp;一次2面挂&nbsp;三次一面挂,我宣布腾讯是世界上最难进的互联网公司然后还有一些零零散散的中小厂,但是数量比较少,约面大多数都是大厂。整体的战况非常惨烈,面试机会少,就算面过了也需要和各路神仙横向对比,很多次我都是那个被比下去的人,不过这也正常,毕竟谁会放着一个985的硕士不招,反而去招一个双非读化学的小子感觉现在互联网对学历的要求越来越高了,不仅仅要985还要硕士了,双非几乎没啥生存空间了,我感觉未来几年双非想要进大厂开发的难度应该直线上升了,唯一的打法还是从大二刷实习,然后苟个转正,不然要是去秋招大概率是炮灰。而且就我面字节这么多次,已经开始问很多ai的东西了,你一破本科生要是没实习没科研懂什么ai啊,纯纯白给了
不知名牛友_:爸爸
秋招你被哪家公司挂了?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务