Raucous rockers-two-dimensional backpack

Source: Internet
Author: User

Raucous rockers

DescriptionYou just inherited the copyright of the unpublished N (1 <= n <= 20) songs recorded by the popular "rock and rock" band. You plan to select some songs from them and release m (1 <= m <= 20) CDs. Each CD can contain up to T (1 <= T <= 20) Minutes of music. A song cannot be divided into two CDs. Unfortunately, you are a fan of classical music and do not know how to determine the artistic value of these songs. Therefore, you decided to choose the following criteria: (1) the song must appear on the CD disk in the order of creation time. (2) select as many songs as possible. InputThe first row: Three integers n, T, M; the second row: N integers, indicating the length of each song, arranged in the order of creation time. OutputAn integer that represents the maximum number of music records that can be loaded into m CD disks. Sample Input4 5 2 4 4 2 Sample output3. Conclusion: although it looks like a two-dimensional backpack problem, it still takes a lot of time to compile it. The reason is that you are not clear about the idea and cannot calm down and look at the routine. Fortunately, I finally understood the routine and completed myself.

Code

  1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 int n,m,t,f[21][21][21];
5 int a[21];
6 int main(){
7 memset(f,0,sizeof(f));
8 FILE *in,*out;
9 in=fopen("input1.txt","r");
10 out=fopen("output.txt","w");
11 fscanf(in,"%d%d%d",&n,&t,&m);
12 int i,j,k;
13 for(i=1;i<=n;i++)
14 fscanf(in,"%d",&a[i]);
15 memset(f,0,sizeof(f));
16
17 for(i=1;i<=m;i++)
18 for(j=1;j<=n;j++)
19 for(k=0;k<=t;k++)
20 if(k>=a[j])
21 {
22 f[i][j][k]=f[i][j-1][k-a[j]]+1;
23 if(f[i][j][k]<f[i-1][j][t])
24 f[i][j][k]=f[i-1][j][t];
25 }
26 else
27 {
28 f[i][j][k]=f[i-1][j][t];
29 if(f[i][j][k]<f[i][j-1][k])
30 f[i][j][k]=f[i][j-1][k];
31 }
32
33
34 fprintf(out,"%d\n",f[m][n][t]);
35 fclose(in);
36 fclose(out);
37 return 0;
38 }
39

F [I] [J] [k] stands for: I am a CD, I am a j song, and I am a CD with K space available, up to the number of tracks to be recorded. A [J] is the length of song J. It can be transferred by the following States:

If (k> = A [J]) f [I] [J] [k] = max {f [I] [J-1] [k-A [J] + 1, f [I-1] [J] [T]);

Else f [I] [J] [k] = max {f [I-1] [J] [T], F [I] [J-1] [k]};

F [I] [J-1] [k-A [J] + 1 -- add the song J to the maximum number of recorded tracks on the I CD;

F [I-1] [J] [T] -- do not put the song J into the I disk, then look at the previous I-1 CD put J the maximum number of recorded songs;

F [I] [J-1] [k] -- the maximum number of recorded tracks for the first J-1 song on the first I CD.

 

 

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.