Total score
Description
The more students score in our usaco competition, the more happy we are. We try to design our competition so that people can score as much as possible. This requires your help.
We can select a competition question from several categories. Here, a "category" refers to a competition question set. Solving the question in the set requires the same time and the same score. Your task is to write a program to tell usaco staff how many questions should be selected from each category, so that the total time for solving the questions is within the time specified by the competition and the total score is the maximum.
Enter the competition time, M (1 <= m <= 10,000), and N (1 <= n <= 10,000 ). The next line will contain two integers: The first integer represents the score (1 <= Points <= 10000) that can be used to solve this problem ), the second integer indicates the time required to solve this problem (1 <= minutes <= 10000 ). Your program should determine how many questions should be selected from each "category" to maximize the score during the competition.
Input
Row 1st: two integers, m n, represent the competition time and the number of question "types.
In the next n rows, each line has two integers, indicating the scores and elapsed time of each "category" question.
Output
A single row contains the maximum score that may be obtained in the given limit.
Sample Input
300 4
100 60
250 120
120 100
35 20
Sample output
605 {select two questions from 2nd "categories" and three questions from 4th "categories}
Code
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 int n,m,mark[10001],time[10001],f[10001];
5
6 int main(){
7 int i,j,k;
8 FILE *in,*out;
9 in=fopen("input3.txt","r");
10 out=fopen("output.txt","w");
11 fscanf(in,"%d%d",&m,&n);
12 for(i=1;i<=n;i++)
13 fscanf(in,"%d%d",&mark[i],&time[i]);
14 memset(f,0,sizeof(f));
15
16 for(i=1;i<=n;i++)
17 for(j=m;j>=1;j--)
18 if(f[j]<f[j-time[i]]+mark[i]&&(j>=time[i]))
19 f[j]=f[j-time[i]]+mark[i];
20
21 fprintf(out,"%d\n",f[m]);
22 fclose(in);
23 fclose(out);
24 return 0;25 }
The difference between a full backpack and a 0-1 backpack is that the cycle order is reversed-for the above 18th rows of loops, the 0-1 backpack should be:
For (I = 1; I <= N; I ++)
For (j = 1; j <= m; j ++)
The difference between the two lies in that there is no limit to the number of items in the complete backpack, and each item in the 0-1 backpack has only one. This method of writing a full backpack ensures that every item
You can mount a package without limits. If you add a condition for determining the number of items in the loop of a full backpack, it is converted to multiple backpacks or 0-1.
Backpack. We also need to note that after changing the cyclic order, we need to set "for (j = 1; j <= m; j ++) "to" for (j = m; j> = 1; j --).