P10: backpack problems in usaco

Source: Internet
Author: User
Appendix: backpack problems in usaco

Usaco, short for USA computing Olympus, organizes many global computer competitions.

Usaco trainng is a very suitable Question Bank for beginners. I think it is characterized by a high quality and gradual process of questions. It also has good texts and question analysis. Text knapsack problems is also worth reading.

In addition, usaco contest is a global competition series organized by usaco all the year round. noip contestants are also recommended here.

I have sorted out the questions related to backpacking in usaco training, which can be used as a good exercise. I recommend the plus sign, which I think is more challenging for noip players.

Question list
    • Inflate (+) (Basic 01 backpack)
    • Stamps (+ )(!) (Challenging for beginners)
    • Money
    • Nuggets
    • Subsets
    • Rockers (+) (another interesting "two-dimensional" backpack problem)
    • Milk4 (!) (It is difficult to solve the problem with pure DP due to a strange question about the backpack)
Question Summary

The following is a full version of usaco, including myProgram, Which can be found in DD's usaco journey.

Inflate is a weighted 01 backpack problem. That is to say, each item has only one item. You can only choose to put it or not, and each item has a corresponding weight value, the goal is to maximize or minimize the total weight. Its simplest state transition equation is: F [k] [I] = max {f [k-1] [I], f [k-1] [I-V [k] + W [k]}. F [k] [I] indicates the maximum weight that can be obtained from the cost of the first K items. V [k] and w [k] are respectively the cost and weight of the k-th item. We can see that the process of solving f [k] is to update f [k-1] with K items. In fact, we don't need to use a two-dimensional array. We only need to define f [I], and then for each item k, check the size of F [I] and f [I-V [k] + W [k] sequentially. If the latter is larger, update the former. This is a typical Optimization Method in the knapsack problem.

In question stamps, there is no direct limit on the use of each item, but there is a limit on the total amount of items used. Find the size of the first backpack that cannot contain the limited item. (It can be equivalent) f [k] [I] indicates that the first K items constitute a backpack with the size of I, and the minimum number of items is required. Then f [k] [I] = min {f [k-1] [I], F [k-1] [I-j * s [k] + J }, J is the number of K items selected. This equation can be used in the same way as the above to process it into one dimension. When solving the problem, set a rough loop upper limit, that is, the maximum number of items that can be multiplied by the largest number of items.

Money is a problem with multiple backpacks. That is, each item can be used for an unlimited number of times. The total number of different solutions for forming a backpack. Basically, you just need to change the min in the ordinary multi-backpack equation to sum.

The Nuggets model also has multiple backpacks. The maximum size (which may not exist) of the backpack cannot be fit for the given item ). Just according to "if I and j are mutually qualitative, then the Indefinite Equation I * x + y * j = n about x and y must have a positive integer solution, n> I * j "indicates the upper limit of a loop. Subsets and problems are equivalent to the number of solutions for calculating the number of 01 backpacks whose size is N * (n + 1)/4 when the item size is the first n natural numbers.

Rockers can use the idea of solving the knapsack problem to design a solution. My state transition equation is as follows: F [I] [J] [T] = max {f [I] [J] [T-1], f [I-1] [J] [T], F [I-1] [J] [T-time [I] + 1, f [I-1] [J-1] [T] + (T> = time [I])}. F [I] [J] [T] indicates the maximum number of songs that can be put into the first song with J complete disks and a t-minute disk, T is the maximum capacity of a disc, and T> = time [I] is a bool value converted to int value 0 or 1. However, I later discovered that the state and equation I designed at that time were a little inefficient. If I changed to this: F [I] [J] = (A, B), it indicates that
Selecting J in the song requires a complete CD and a B-minute CD, which greatly reduces the time-space complexity. This method of setting the state value to two-dimensional is worth noting.

Milk4 is the most difficult of these type of backpack problems. Many people cannot solve the problem by using the pure DP Method. Instead, they use iterations to deepen the search for buckets used by enumeration and convert them into multiple backpack problems and then DP. Due to the weak data of usaco, the depth of iteration is very small, which can also be AC, but we can still use the pure DP Method to Solve it perfectly. Set f [k] to the minimum number of barrels per unit of milk for weighing. We can use a method similar to multiple backpacks to repeatedly update the f array to obtain the minimum value. However, the difficulty lies in how to output the smallest Lexicographic Order. We can record pre_f [I] And pre_v [I] for each I. This indicates that the process of getting milk in unit I is to add several barrels of milk numbered pre_v [I] With pre_f [I. In this way, you can obtain
Complete Solution to I unit milk. To minimize the Lexicographic Order of the solution, we can compare the stored solution and the new solution each time we find a solution with the same number of buckets, and then decide whether to update the solution. In order to make this fast, the F array is updated using buckets of various sizes. The usaco official solution is exactly this idea. If you think the above text is hard to understand, you can read the official program or my program.

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.