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