Competition scores

Title Description

The more students score in our USACO the race, the more we are happy.
We try to design our contests so that people can score as many points as possible, we need your help.
We can choose from several types of title race, one where "species" refers to a collection of contest topic, to solve a set of topics that require the same amount of time and energy to get the same score. Your task is to write a program to tell USACO staff, how many topics should be selected from each category, making the total time to solve the problem of competition in the prescribed time and score maximum. Inputs include race against time, M (1 <= M < = 10,000) ( Do not worry, you go to the training camp will have a long game) and N, the number of "species" of 1 <= N <= 10,000. Each row comprises two integers later to describe a "genre":
The first solution described integer fraction of this topic can be obtained (1 <= points <= 10000 ), the second integer instructions needed to address this topic time (1 <= minutes <= 10000 ).
Your program should determine how much road we should select topics from each "kind" makes it possible to get the maximum score in a race against time.
Any number of topics from the "type" may be any non-negative number (0 or more).
Calculate the maximum score possible get.

· (See the feeling here is a backpack, qwq)

Entry

Line 1: M, the number of "type" of the time and subject N-- race.
Of 2-N + 1 rows: two integers: each "type" topic scores and time consuming.

Export

A single line that includes the biggest score in a given limit may get.

Sample input

300 4
100 60
250 120
120 100
35 20

Sample Output

605

Thinking

This question is what, let us ask of the biggest scores, see the data range is only a two-dimensional DP.
\ [O (N ^ 2) over a million now, no problem. \]

  • [x] 其实我还见过

\ [O (N ^ 2) more than a million of it. \]
Let us analyze this two-dimensional DP:

The contest topic that time is actually converted into a backpack knapsack problem is capacity, N N expressed items

Everything has mass and volume. He said he would at most, can not only choose a group, so it can choose a number. In fact, the total value of these items is the answer of the given data.

Correct answer is so completely backpack ah. (What exactly backpack please refer to Baidu Encyclopedia)

Specific point of it?

But wait a minute.

Speaking about the full backpack.

First first two-dimensional style of play:

Fij i set up a front or items placed in a maximum value of j-capacity backpack obtained, whether the selected program is the first i items of.

You can then draw the state transition equation:

When these items when selected

f[i][j] = f[i][j-a[i]]+w[i];

Election time to put a [i] This time-consuming to lose plus score.

When these items do not choose the time

f[i][j] = f[i-1][j];

Do not choose when he retained the one optimal solution.

So the code structure came out.

for(int i = 1; i <= n ; i++){
        for(int j = 1; j <= m ;j++){
            if(j < a[i]) f[i][j] = f[i-1][j];
            else if(f[i-1][j] > f[i][j-a[i]]+w[i]) f[i][j] = f[i-1][j];
            else f[i][j] = f[i][j-a[i]]+w[i];
        }
    }

Cycle i -> n:

Cycle j -> m

When j <a [i], is already ensured optimal solution thus not selected.

When a solution has been greater than the current optimal solution, then also it can not be selected.

Other cases should be selected.

The answer on fnm, output it.

Introduction to put a state transition equation:

f[i][j]=max{f[i-1][j-k*c[i]]+k*w[i]|0<=k*c[i]<=j}

Then revisit the one-dimensional style of play:

Sometimes people hate the topic, deliberately let you fry memory, so two-dimensional, but the backpack.

This time to use one-dimensional style of play (two-dimensional array when the premise is greater than 5000 * 5000, a two-dimensional RE ).

In fact, i do not have this dimension, only j, space complexity greatly decreased.

Cycle to change it.

1 -> n unchanged

w [i] -> m to avoid duplication

One-dimensional thinking:

This selected items:

if(f[j-w[i]]+a[i] > f[j]){
    f[j] = f[j-w[i]]+a[i];
 }

The current update is greater than before.

Otherwise quit.

Without the election: No deal.

One-dimensional code architecture came out:

for(int i = 1; i <= n ; i++){
        for(int j = w[i]; j <= m ;j++){
            if(f[j-w[i]]+a[i] > f[j]){
                f[j] = f[j-w[i]]+a[i];
            }
        }
    }

Comprehensive look at the state transition equation above

f[j] = max(f[j],f[j-w[i]]+a[i]);

The answer lies f [m] output it.

  • (Indeed better than playing more one-dimensional, in fact, like ah)

to sum up

Use different codes for different topics.

This question is for memory, I wrote a dimension before.

Dimensional jj 91 points, see ancestors ah.

In the final analysis this is only a backpack full of question templates, and finally ranked first in the A grades.

  • (I'm too weak, but also ... qwq)

Learning to know exactly backpack:

  • [X] What is dynamic programming.

  • [X] What is a recursive algorithm.
  • [X] What is the memory search.
  • [X] What is a recursive algorithm.

In order to better understand.

End.

Guess you like

Origin www.cnblogs.com/wangshengjun/p/11220644.html