The Fewest Coins POJ, fewestpoj

Source: Internet
Author: User

The Fewest Coins POJ, fewestpoj

The Fewest Coins POJ-3260

Full backpack + multiple backpacks. The basic idea is to find the minimum number of coins used for "paying" the specified amount of money and "looking for" the specified amount of money separately through a backpack, and then enumerate the money you are looking, then the money is determined, and an answer can be obtained for each piece of money, so you can enumerate all the possible money to obtain the maximum value of the answer.

Here is a proof of the maximum amount of money to find. If you do not know, you can also create one (for example, 10000 of the following, pay attention to space, and try 5000 ).

Error records:

4. During Multi-bag optimization, divide by 2 and write it as <= 2, WA

3. Multiple backpacks are violent and not optimized. TLE

2. The dp array is initialized to 0x3f3f3f, but the anss (Final answer) is initialized to 0x6fffffff, and finally anss! = 0x6fffffff, but even if there is no answer, anss will be updated by 0x3f3f3f, WA

1. Only 1000 yuan for enumeration, WA

 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int n,m,T,v[110],c[110],p[11000],ans[21200],anss=0x3f3f3f3f; 6 int main() 7 { 8     int i,j; 9     scanf("%d%d",&n,&T);10     memset(ans,0x3f,sizeof(ans));11     memset(p,0x3f,sizeof(p));12     for(i=1;i<=n;i++)13         scanf("%d",&v[i]);14     for(i=1;i<=n;i++)15         scanf("%d",&c[i]);16     p[0]=0;17     for(i=1;i<=10000;i++)18     {19         for(j=1;j<=n;j++)20             if(i>=v[j])21                 p[i]=min(p[i],p[i-v[j]]);22         p[i]++;23     }24     ans[0]=0;25     for(i=1;i<=n;i++)26     {27         m=1;28         while(c[i]>0)29         {30             if(m>c[i])    m=c[i];31             c[i]=c[i]-m;32             for(j=T+10000;j>=v[i]*m;j--)33             {34                 ans[j]=min(ans[j],ans[j-v[i]*m]+m);35                 if(j>=T)36                     anss=min(anss,ans[j]+p[j-T]);37             }38             m<<=1;39         }40     }41     if(anss!=0x3f3f3f3f)42         printf("%d",anss);43     else44         printf("-1");45     return 0;46 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.