Description
There are n programs {1, 2 ,..., N} is stored on a tape with a length of L. To store program I, the tape length must be Li, 1 <= I <= n. Now I want to ask the students to design a storage solution so that they can store as many programs as possible on tape. The maximum utilization of tapes is also required to ensure the maximum number of stored programs.
-
Input
-
The first behavior is an integer T, indicating that there are T groups of test data.
For each group of test data, the first line is a positive integer (no more than 106), indicating the length of the tape; the second line is a positive integer N (no more than 100), indicating that there are n programs; the third row is n positive integers (the number of each digit cannot exceed 100). The number of I (LI) indicates that the length of the tape to be occupied is Li, and each digit is separated by spaces.
-
Output
-
For each group of test data, the first row is a positive integer, indicating the maximum number of programs that the tape can store. The second row is a positive integer, indicating the total length of the tapes that these programs store.
-
Sample Input
-
1
-
60
-
10
-
2 3 8 10 12 13 16 21 23 80
-
Sample output
-
6
-
60
Dynamic Planning, similar to a backpack Problem
#include <stdio.h>#include <string.h>main(){int number,te;long int l;int a[101];int n;int i;int j;int cnt[10001];int sum;scanf("%d",&number);for(te=1;te<=number;te++){ memset(cnt,0,sizeof(cnt));scanf("%ld",&l);scanf("%d",&n);for(i=1;i<=n;i++)scanf("%d",&a[i]); sum=0; for(i=1;i<=n;i++)sum+=a[i];if(sum<l)l=sum;if(sum==l){printf("%d\n",n);printf("%d",sum);goto ABC;} for(i=1;i<=n;i++)for(j=l;j>=0;j--){if(cnt[j-a[i]]+1>cnt[j]&&j-a[i]>=0)cnt[j]=cnt[j-a[i]]+1;elsecnt[j]=cnt[j];}for(j=l;j>=0;j--)if(cnt[j]!=0)break; printf("%d\n",cnt[j] );printf("%d",j);ABC: printf("\n");}}