UVA 624 record path of 0-1 backpack

Source: Internet
Author: User

Test instructions
For a long journey, you have an N-minute tape, but all your favorite songs are on CDs,
You need to record all the CDs in the tape and ask which songs you can use to make the most of your tape space.
Enter the tape capacity N and the number of songs X, and the next X number represents the length of each song.
The output is which songs, and the total length of time.

Solving:

0-1 backpack, you can find out the maximum number of places to go in.
And then use an array to record the backpack capacity of J when put in the first few items, you can export spicy.
The first array is not big enough RE spicy

2333 Although the simple question but write it out still very excited AH

#include <bits/stdc++.h>#definell Long Longusing namespacestd;Const intMAXN =100010;Const intINF =0x3f3f3f3f;intd[maxn],a[ -],P[MAXN];intMain () {intN, Num;  while(SCANF ("%d", &n)! =EOF) {scanf ("%d", &num);  for(inti =1; I <= num; i + +) {scanf ("%d", &A[i]); }        intD[MAXN] = {0}, P[maxn] = {0};  for(inti =1; I <= num; i + +) {             for(intj = N; J >= A[i]; J--) {                if(D[j-a[i]] + a[i] >D[j]) {D[j]= D[j-a[i]] +A[i]; P[J]=i; }            }        }        inti = d[n], cnt =0, ans[ -] = {0};  while(I >0) {ans[cnt++] =A[p[i]]; I= i-A[p[i]]; }         for(intj = cnt-1; J >=0; J--) printf ("%d", Ans[j]); printf ("sum:%d\n", D[n]); }    return 0;}

UVA 624 record path of 0-1 backpack

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.