1286 optimal storage

Source: Internet
Author: User

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.


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.


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
2 3 8 10 12 13 16 21 23 80
Sample output


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");}}


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.