[Blue] ALGO-31 Bridge Cup happy Jinming

Subject 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], k items were selected, followed by numbers j_{1},j_{2},j_{3},...,j_{k}, then the request is the sum of:

v[j_{1}]*w[j_{1}]+v[j_{2}]*w[j_{2}]+...+v[j_{k}]*w[j_{k}]. (Where * is the multiplication sign)

Please help Jinming designed to meet the requirements of a shopping list.

Enter a description:

A first input line, 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 indicates the degree of importance of the item (1-5)).

Output Description:

The maximum output is 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 2

Sample output:

3900

Problem-solving ideas:

01 knapsack problem. After DP [i] [j] denotes the front i total items does not exceed the element j, the price of the item, the maximum values ​​and the sum of the products of importance. Consider two cases when buying items: ① If the currently selected item price below approximately Maximum permissible total element j, is not considered later, dp [i] [j] = dp [i-1] [j]; ② If the volume of the currently selected item is not greater than the total maximum permissible j membered, buy or not to buy considering two states, select the item price and product importance that the sum is largest in the case where: dp [i] [j] = max (dp [i-1] [j], dp [i-1] [ja] + t). DP final output [m] [n] that is purchased items and m does not exceed the total maximum value of n Price important product synthesis.

AC Code:

#include <bits/stdc++.h>
using namespace std;
#define Up(i,a,b) for(int i = a; i <= b; i++)
const int maxm = 31;
const int maxn = 30001;

int dp[maxm][maxn];  //dp[i][j]表示前i件物品的总价不超过j元时价格与重要程度乘积综合的最大值

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int n,m;    //总钱数n,渴望购买物品数m
    cin >> n >> m;
    Up(i,1,m)
    {
        int x,y;  
        cin >> x >> y;
        Up(j,1,n)
        {
            if(x > j)
            {
                dp[i][j] = dp[i-1][j];
            }
            else
            {
                dp[i][j] = max(dp[i-1][j],dp[i-1][j-x]+x*y);
            }
        }
    }
    cout << dp[m][n] << endl;
    return 0;
}

 

Published 415 original articles · won praise 118 · Views 400,000 +

Guess you like

Origin blog.csdn.net/weixin_42449444/article/details/102997318