POJ Charlie ' s change Charlie's conversion (multiple backpack, micro-variant)

Source: Internet
Author: User

Test instructions: Given the 4 kinds of coins on the body, respectively, 1, 5, 10, 25 denominations have more than one, requires the formation of the denomination p coins as much as possible. Outputs the respective number of 4 coins that comprise p.

Idea: multiple backpacks, 300+ms. Use the 01 Backpack + binary method. Record the number of all coins and the number of 4 coins, and note the differences between the initialized DP arrays.

1#include <iostream>2#include <cstdio>3#include <cstring>4 #defineLL Long Long5 using namespacestd;6 7 Const intn=10010, inf=-1;8 intcoin[4]={1,5,Ten, -};9 intdp[n][5], num[5];Ten intp; One  A voidcal () - { -Memset (DP,0,sizeof(DP)); the      for(intI=1; i<=p; i++) dp[i][0]=INF; -      for(intI=0; i<4; i++) -     { -         intk=1, tmp=Num[i]; +          while(1) -         { +             if(k>tmp&&tmp) k=tmp; A             Else if(k>tmp) Break; at  -              for(intJ=p; j>=k*coin[i]; j--) -             { -                 if(dp[j-k*coin[i]][0]!=inf &&dp[j][0]<dp[j-k*coin[i]][0]+k) -                 { -dp[j][0]=dp[j-k*coin[i]][0]+K; in                      for(intC=1; c<=i+1; C + +)//Number of copies -dp[j][c]=dp[j-k*Coin[i]] [C]; todp[j][i+1]+=k;//Number of Additions +                 } -             } thetmp-=K; *k<<=1; $         }Panax Notoginseng     } -     if(dp[p][0]==inf) printf ("Charlie cannot buy coffee.\n"); the     Elseprintf"Throw in%d cents,%d nickels,%d dimes, and%d quarters.\n", dp[p][1], dp[p][2], dp[p][3], dp[p][4] ); + } A  the  + intMain () - { $  $     //freopen ("Input.txt", "R", stdin); -      while(cin>>p,p) -     { the         intCnt=0; -          for(intI=0; i<4; i++)Wuyi         { thescanf"%d", &num[i]); -cnt+=num[i]*Coin[i]; Wu         } -         if(cnt<p) About         { $printf"Charlie cannot buy coffee.\n"); -             Continue; -         } - cal (); A     } +     return 0; the}
AC Code

POJ Charlie ' s change Charlie's conversion (multiple backpack, micro-variant)

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.