Blue Bridge Cup ALGO-31 training algorithm happy Jinming

Training algorithm happy Jinming  

Time limit: 1.0s Memory Limit: 256.0MB

 

Problem Description
  Jinming I am very happy, the family purchased a new house on the key essentials, there is a new room dedicated himself very spacious room. He became even more pleased that my mother yesterday and said to him: "You need room which items to buy, how layout, you have the final say, as long as no more than N dollars on the line." This morning Jinming started doing the budget, but he wanted to buy so many things will certainly exceed the limit N mother yuan. Thus, each item he specifies a degree of importance, and the like is divided into 5: 1 to 5 show an integer, fifth, etc. most important. He also found the price of each item (are integers dollars) from the Internet. He wants without exceeding N element (element may be equal to N) of the sum of the products and the degree of importance of the price of each item maximum.
  The first set price for items j V [j], a degree of importance W [j], a total of k selected items, numbered sequentially as j1, j2, ......, jk, then the required sum is:
  V [J1 ] * w [j1] + v [j2] * w [j2] + ... + v [jk] * w [jk]. (Where * is the multiplication sign)
  Please help Jinming designed to meet the requirements of a shopping list.

 

Input format
  of the input file, line 1, two positive integers, separated by a space:
  N m
  (where N (<30000) represents the total amount of money, m (<25) to a desired number of items purchased.)
  From row 2 to row m + 1, j-th row gives the article number j-1 is the basic data, each line has two non-negative integer
  VP
  (where v represents the price of the item (v <= 10000) , p represents the degree of importance of the article (1 to 5))

 

Output Format
  maximum output file only a positive integer, with the degree of importance of the price of the product does not exceed the total number of items of money of the sum (<100 000 000).

 

Sample input
1000. 5
800 2
400. 5
300. 5
400. 3
200 is 2

 

Sample Output
3900

 

Scale data and conventions

 

Analysis: set f(i, n)to have a total budget in nthe pre-processed element premise imaximum price and degree of importance of the items sum of the products obtained, which is a recurrence relation

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

 

#include <stdio.h>

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

int main()
{
    int N, m;
    int v[30] = { 0 }, p[30] = { 0 };
    int f[30][30005] = { 0 };

    scanf("%d %d", &N, &m);
    for (int i = 1; i <= m; ++i)
        scanf("%d %d", &v[i], &p[i]);

    for (int i = 1; i <= m; ++i)
    {
        for (int n = 0; n < v[i]; ++n)
            f[i][n] = f[i-1][n];
        for (int n = v[i]; n <= N; ++n)
            f[i][n] = max(f[i-1][n], f[i-1][n-v[i]] + v[i] * p[i]);
    }
    printf("%d", f[m][N]);

    return 0;
}

 

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

Guess you like

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