UVA 624 CD (01 backpack)

Source: Internet
Author: User

Cd

You had a long drive by car ahead. You had a tape recorder, but the unfortunately your best music was on CDs. You need to has it on tapes so the problem to solve is:you has a tape N minutes long. How to choose tracks from CD to get the most out of tape space and has as short unused space as possible.


Assumptions:

    • Number of tracks on the CD. does not exceed 20
    • No track is longer than N minutes
    • Tracks do not repeat
    • Length of each track was expressed as an integer number
    • N is also integer

Program should find the set of tracks which fills the tape best and print it in the same sequence as the tracks is stored On the CD

Input

Any number of lines. Each one contains value N, (after space) number of tracks and durations of the tracks. For example from first line in Sample data: N=5, number of tracks=3, first track lasts for 1 minute, second one 3 Minutes, next one 4 minutes

Output

Set of tracks (and durations) which is the correct solutions and string "sum:" and sum of duration times.

Sample Input

5 3 1 3 410 4 9 8 4 220 4 10 5 7 490 8 10 23 1 2 3 4 5 745 8 4 10 44 43 12 9 8 2

Sample Output
1 4 sum:58 2 sum:1010 5 4 sum:1910 1 2 3 4 5 7 sum:554 ten 9 8 2 sum:45


Simple 01 backpack, but to output the path, the first write feels particularly troublesome to create a two-dimensional array to record whether the object is selected.

Test instructions: The first number is the distance, the second number is the number of songs, the number behind is the time to listen to each song. Ask for as long as possible in this time to listen to the song, each song must listen to complete, the total number of songs to listen to the length of time.
Output the time of all listening songs, in order of the input order, and the last total time.

Attached code:

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 intMAXS (intAintb)6 {7     returnA>b?a:b;8 }9 intMain ()Ten { One     inti,j,n,m; A     inta[ -],dp[10005]; -     ints[ -][10005]; -      while(~SCANF ("%d%d",&m,&N)) the     { -          for(i=0; i<n; i++) -scanf"%d",&a[i]); -Memset (DP,0,sizeof(DP)); +memset (s),0,sizeof(s)); -          for(i=n-1; i>=0; i--)//reverse sequence input, in order to be able to use the positive sequence later +              for(j=m; j>=a[i]; j--)//The simplest of 01 backpacks A                 if(dp[j]<dp[j-a[i]]+A[i]) at                 { -                     //Dp[j]=maxs (Dp[j],dp[j-a[i]]+a[i]); //This write will be an error!!! I don't know why = may be a computer problem, tangled up an afternoon -dp[j]=dp[j-a[i]]+A[i]; -S[I][J] =1; -                 } -          for(i=0, J=dp[m]; I<n,j>0; i++)//Output Path in         { -             if(S[i][j]) to             { +printf"%d", A[i]); -j-=A[i]; the             } *         } $printf"sum:%d\n", Dp[m]);Panax Notoginseng     } -     return 0; the}

UVA 624 CD (01 backpack)

Related Article

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.