Blue Bridge Cup ALGO-30 algorithm training entrance examination

Entrance examination training algorithm

Time limit: 1.0s Memory Limit: 256.0MB

 

Problem Description
  chen was a gifted child, his dream is to become the world's greatest physician. To this end, he wanted to worship in the vicinity of the most prestigious physician as a teacher. Physician in order to determine his qualifications, gave him a problem. Physicians took him to a cave full of herbs says to him:. "Children, this cave has some different herbs, picking each plant requires some time, each plant has its own value and I will give you some time, during this time, you can pick some herbs maximum if you are a smart kid, you should be taken to make the total value of herbs. "
  If you are chen, you can accomplish this task ?

 

Input format
  of the first line has two integers T (1 <= T <= 1000) and M (1 <= M <= 100), separated by a space, and T represents a total time can be used herbs, M for the number of herbs cave. Next M lines each comprising two integers between 1 to 100 (including 1 and 100), respectively, represent the value of a strain picked herbs time this strain and herbs.

 

Output formats
  include line, which contains a single integer representing within the stipulated time, you can adopt a maximum total value of herbs to.

 

Sample input
70. 3
71 is 100
69. 1
. 1 2

 

Sample output
3

 

Data size and conventions
  for data of 30%, M <= 10;
  for all the data, M <= 100.
 

Analysis: set f(i, t)indicates a total time of tprocessing the first to the imaximum value of the total strain herbs can be obtained, which is a recurrence relation

f(i, t) = \begin{cases} \max \{ f(i-1, t), f(i-1, t-t_i) + v_i \} & \text{ if } t \ge t_i \\ f(i-1, t) & \text{ if } t < t_i \\ 0 & \text{ if } i = 0, t \ge 0 \end{cases}

 

#include <stdio.h>

int max(int a, int b)
{
    return a > b ? a : b;
}

int main()
{
    int T, M;
    int time[105] = { 0 }, value[105] = { 0 };
    int f[105][1005] = { 0 };

    scanf("%d %d", &T, &M);
    for (int i = 1; i <= M; ++i)
        scanf("%d %d", &time[i], &value[i]);

    for (int i = 1; i <= M; ++i)
    {
        for (int t = 0; t < time[i]; ++t)
            f[i][t] = f[i-1][t];
        for (int t = time[i]; t <= T; ++t)
            f[i][t] = max(f[i-1][t], f[i-1][t-time[i]] + value[i]);
    }
    printf("%d", f[M][T]);

    return 0;
}

 

Published 221 original articles · won praise 40 · views 40000 +

Guess you like

Origin blog.csdn.net/liulizhi1996/article/details/104005999