UVa 624 CD (0-1 backpack)

Source: Internet
Author: User
Tags bool printf time limit

624-cd

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=565

You are have a long drive by car ahead. You have a tape recorder, but unfortunately your best music are on CDs. You are need to have it in tapes so problem to solve is:you a have N tape long. How to choose tracks from CD to get most out of tape spaces and have as short unused spaces 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 are 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 are 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 the-I-data:n=5, number of tracks=3, track lasts for 1 minute, second one 3 minutes, Next one 4 minutes

Output

This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45378.htm

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

Sample Input

5 3 1 3 4 4 9 8 4 2 4 5 7 4 8 1 2 3 4 5 7 45 8 4 A

Sample Output

1 4 sum:5
8 2 sum:10
5 4 sum:19 1 2 3 4 5 7 sum:55 4 9 8
2 sum:45

Ps:special judge

The output in the code is reversed, to save the available stack on the first.

I write this purely for the memory of the  
record path: vis[i++][j--] = Max2 (Dp[j], Dp[j-weight[i]] + price[i);///MAX2 is a bool-type function that assigns the maximum value to dp[j. Returns True when the second argument >= the first argument.  
Print Path: PRINTF2 (Vis[i--][j-=weight[i]]);///vis[i][j] When True, print weight[i].

Complete code:

/*0.019s*/
    
#include <cstdio>  
#include <cstring>  
const int MAXN = 10005;  
    
int val[25], DP[MAXN];  
BOOL VIS[25][MAXN];  
    
int main ()  
{  
    int n, M, I, J;  
    while (~SCANF ("%d%d", &n, &m))  
    {  
        memset (DP, 0, sizeof (DP));  
        memset (Vis, 0, sizeof (VIS));  
        for (i = 0; i < m ++i)  
            scanf ("%d", &val[i]);  
        for (i = 0; i < m. ++i) for  
            (j = n; j >= Val[i];--j)  
                if (dp[j-val[i)] + val[i] >= dp[j])  
                {
  
   DP[J] = Dp[j-val[i]] + val[i];  
                    VIS[I][J] = true;  
                }  
        j = N;  
        for (i = m-1 i >= 0; i)  
            if (Vis[i][j])  
            {  
                printf ("%d", Val[i]);  
                J-= Val[i];  
            }  
        printf ("sum:%d\n", Dp[n]);  
    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.