Dynamic Planning for contract baseball free players

Source: Internet
Author: User

Suppose you are the general manager of a major league baseball team. You need to sign in to some free players during the offseason. The team boss gives you a budget of $ X, and you can use less than $ X to sign in players, but if you overhead, the team boss will fire you.

You are considering entering players in n different positions. There are P free players in each position for you to choose from. Because you do not want to be too bloated in any position, you can check in at most one player at a location (if you do not check in any player at a specific position, it means you plan to continue using existing players ).

To determine the value of a player, you decide to use a statistical evaluation indicator called "vorp" or "Player replacement value. The higher the player's vorp value, the higher the value. However, the contract price for a player with a high vorp value is not necessarily higher than that of a player with a lower vorp value, because there are other factors that affect the contract price.

For each selectable free player, you know the following information:

1. Position of the attacker. 2. His contract fees. 3. His vorp.

Design a player selection algorithm. The total contract cost is no more than usd x, and the total vorp of players is the largest. You can assume that the contract cost for each player is an integer multiple of the USD 0.1 million. The algorithm should output the total vorp value of the contracted players, total contract fees, and the list of players. Analyze the time and space complexity of the algorithm.

Thinking and analysis: This is obviously an improved backpack problem. The total capacity of a backpack is equal to the total capacity of no more than usd x. The total vorp is the largest, which is equivalent to the total value of the backpack. The contract cost for each player is equivalent to the weight of each item. Now we want to provide the player selection solution with the greatest total value without having to sign the contract for less than usd x. We can improve the dynamic planning algorithm based on the 0-1 backpack. Here, each position (equivalent to each item) has P players. Then we need to add a loop to find out the most valuable among the P players while finding the maximum value solution. Now we can portray recursion.

RecursionIs:

The Code is as follows:(The comments in this Code are compared with the problem related to the backpack. The similarities with the problem are explained in detail. If you have any questions about the problem, open the above link)

# Include <iostream> # include <time. h> using namespace STD; # define N 10 struct player {// int cost of the player struct; // int vorp of the amount spent on hiring players; // value of the player }; void free_agent_vorp (struct player ** P, int N, int P, int X) {// set the value of V [I] [J] int ** V, ** who, I in the sequence from bottom to top; V = new int * [n + 1]; for (I = 0; I <= N; I ++) {v [I] = new int [X];} WHO = new int * [n + 1]; for (I = 0; I <= N; I ++) {who [I] = new int [X];} // two-dimensional array V [I] [J] indicates that the total value of position I does not exceed the total cost J (equivalent to the total value of the backpack) // two-dimensional array who [I] [J] Indicates that the total cost of position I cannot exceed J players for (INT x = 0; x <= x; x + = 10) // 10 = 0.1 million {// first put V [N] [x] V [N] [x] =-0x7fffffff; who [N] [x] = 0; // If X is less than V [N] [X], the corresponding value is set to 0. Otherwise, for (int K = 0; k <= P; k ++) {If (P [N] [K]. cost <= x & P [N] [K]. vorp> V [N] [x]) {v [N] [x] = P [N] [K]. vorp; who [N] [x] = K ;}}// place the remaining N-1 locations. For (I = N-1; I> = 1; I --) {for (INT x = 0; x <= x; x + = 10) // X indicates the current total cost (equivalent to the current total weight J) {v [I] [x] = V [I + 1] [x]; who [I] [x] = 0; For (int K = 0; k <= P; k ++) {// note that K = 0 is an existing player, k = 0 indicates that the player does not need to be replaced. // P [I] [K]. cost represents the cost of the K player of position I (equivalent to the weight of the K item of position I) // P [I] [K]. vorp represents the value of position I's K players (equivalent to position I's K item value) if (X-P [I] [K]. cost> = 0 & V [I + 1] [x-P [I] [K]. cost] + P [I] [K]. vorp> V [I] [x]) {// select the Player V [I] [x] = V [I + 1] [x-P [I] [k] with the largest total value in the total cost range. cost] + P [I] [K]. vorp; who [I] [x] = K ;}}} cout <"max value =" <v [1] [x] <Endl; int AMT = x; // use a variable to save the total cost (equivalent to the total weight) for (I = 1; I <= N; I ++) {int K = who [I] [AMT]; // The cost of position I is the Kif (K! = 0) // when K is not 0, description: The k-th player to sign the contract position I {cout <"no" <I <"position" <"no" <k <"individual-> "; AMT-= P [I] [K]. cost; // total fees after a player is signed. (Equivalent to the total remaining weight) }}cout <"the total money spent is" <X-AMT <Endl;} void main () {// srand (unsigned) Time (null); struct player ** P; int n = 10, P = 8, x = 300; // X indicates the total cost (total backpack weight X); P = new player * [n + 1]; for (INT I = 0; I <= N; I ++) {P [I] = new player [p + 1] ;}for (I = 0; I <= N; I ++) {for (Int J = 0; j <= P; j ++) {P [I] [J]. cost = 10 * (J + 1); // The Player fee (equivalent to the weight of the item, which is not used at location 0) P [I] [J]. vorp = rand () % 300; // value of the player (equivalent to the value of the item, set position 0 to null)} free_agent_vorp (p, N, P, x );}
Summary: The algorithm runs at O (NXP) and the storage capacity is O (nx ). this problem is similar to the problem of a backpack. It can be said that it is an upgraded version of the problem, and there is a loop, but the essence is the same.

Zookeeper

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.