POJ 2581 Exact Change Only (dp)

Source: Internet
Author: User

POJ 2581 Exact Change Only (dp)

Language:DefaultExact Change Only
Time Limit:1000 MS Memory Limit:65536 K
Total Submissions:2584 Accepted:883

Description

Boudreaux reached over and shook awake Thibodeaux, who had dozed off somewhere in New Mexico. "Where we? "Thibodeaux groggily yawned.

"Not in Vegas, I gua-ran-tee, but cocould you get my knapsack? "Boudreaux asked, gesturing to the worn, leather backpack in the back seat of their cherry red Ford Miata.

"Why, is there a problem? "

"Just hand me my knapsack, problem or not ."

Thibodeaux complied, glancing up as Boudreaux slowed the car to a stop in a line of vehicles approaching a toll booth. "$1.65 -- Exact change only," Thibodeaux read the yellow sign on the front of a small wooden building occupied by a lone toll booth operator. "I have to get $1.65 in exact change? "Thibodeaux asked, digging through the knapsack," all I have are ten quarters, four dimes, and three pennies. I don't have any nickels ..."

"Just give me five of the quarters and the four dimes," Boudreaux replied, holding out his hand.

"Oh yeah," Thibodeaux said, handing over the coins, "that does add up to $1.65. I wish there were an easy way to figure out if you have an exact monetary amount, given a set of coins."

"Hmmm," Boudreaux shrugged, "sounds like a good programming problem ."

Input

Input to this problem will consist of a (non-empty) series of up to 100 data sets. each data set will be formatted according to the following description, and there will be no blank lines separating data sets.

A single data set has 1 component:

Start line-A single line:
A B C D E

Where:
A: (0.01 <= A <= 5.00) is a decimal number (to two decimal places) of a monetary amount.
B: (0 <= B <= 100) is an integer number of quarters (one quarter = $0.25 ).
C: (0 <= C <= 100) is an integer number of dimes (one dime = $0.10 ).
D: (0 <= D <= 100) is an integer number of nickels (one nickel = $0.05 ).
E: (0 <= E <= 100) is an integer number of pennies (one penny = $0.01 ).

Output

For each data set, there will be exactly one line of output. if there exists one or more subsets of the given coins whose values add up to the given monetary amount exactly, the output will be a single line in the form:

A B C D

Where A is the number of quarters, B is the number of dimes, C is the number of nickels, and D is the number of pennies, for the subset with the fewest number of coins. otherwise, the output will be a single line with the statement:
NO EXACT CHANGE

Sample Input

0.45 2 1 1 40.75 3 7 1 75

Sample Output

NO EXACT CHANGE3 0 0 0

Source

South Central USA 2003


It is to enlarge the money by 100 times and then combine it into all. What is the minimum cost for money?

Dp + record path. It is worth mentioning that this question can also pass through four for loops, but I still feel that dp is a little tall.


#include
 
  #include
  
   #include
   
    #includeusing namespace std;#define N 1000int num[5],dp[N],pre[N],all;int op[5]={25,10,5,1};int ans[N];int main(){int i,j;double x;while(~scanf("%lf",&x)){all=(int)(x*100);for(i=0;i<4;i++)scanf("%d",&num[i]);memset(dp,0,sizeof(dp));dp[0]=1;for(i=0;i<4;i++){if(op[i]*num[i]>=all){for(int v=op[i];v<=all;v++)if(dp[v-op[i]]&&(dp[v]==0||dp[v]>dp[v-op[i]]+1)){dp[v]=dp[v-op[i]]+1;pre[v]=v-op[i];}}else{int t=num[i];while(t--){for(int v=all;v>=op[i];v--) if(dp[v-op[i]]&&(dp[v]==0||dp[v]>dp[v-op[i]]+1)){dp[v]=dp[v-op[i]]+1;pre[v]=v-op[i];}}}}if(!dp[all]){printf("NO EXACT CHANGE\n");continue;} memset(ans,0,sizeof(dp)); i=all; while(i) { ans[i-pre[i]]++; i=pre[i]; } printf("%d %d %d %d\n",ans[25],ans[10],ans[5],ans[1]);}return 0;}
   
  
 







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.