1 model (2) day2

Source: Internet
Author: User

Question 1:

Give n items and the number of each item, and find the number of K items. N <= 30

 

Problem solving process:

1. At the beginning, I always thought about the model of combining mathematics, and I couldn't even figure out the result .. Then a burst search is written, and the data is weak. Only one point has timed out.

2. AC algorithm: F [I] [J] indicates the number of solutions for obtaining J-pieces in the first K, and 0, 1, 2 .... P [I], number of accumulative solutions, that is, F [I] [J] = sum (F [I-1] [J-K]) 0 <= k <= min (J, P [I]);

Thinking: if the data range is changed to a larger value, for example, k, n <= 2000, and the number of mod P is calculated, then the original O (N * k ^ 2) considering that the values of F [I] [J] are related to the sum of F [I-1, when F [I] [J] is obtained, a prefix sum is obtained. sum [I] [J] indicates sum (F [I] [0 .... j]) then the equation becomes f [I] [J] = sum [I-1] [J]-sum [I-1] [J-min (J, P [I]) -1]. optimized to O (NK );

 

Question 2:
Divers use special equipment for diving. He has a cylinder with two types of gases: oxygen and nitrogen. The depth of diver dive requires a variety of oxygen and nitrogen. Divers have a certain number of cylinders. Each cylinder has a weight and a gas capacity. A diver needs a specific amount of oxygen and nitrogen to complete his work. What is the minimum cylinder weight required for him to complete his work?
For example, a diver has five cylinders. There are three numbers in each row: oxygen, nitrogen (litre), and cylinder weight:
3 36 120
10 25 129
5 50 250
1 45 130
4 20 119
If the diver needs 5 litre oxygen and 60 litre nitrogen, the total weight is at least 249 (or cylinder ).
Your task is to calculate the minimum weight of the cylinder required by a diver to complete his work.

 

Problem solving process:

1. this is a 01 backpack problem, but the State has changed to three dimensions. Considering that the oxygen and nitrogen can exceed the required oxygen and nitrogen, modify the state, f [k] [I] [J] indicates the first K items. Oxygen is at least I, and nitrogen is at least J's minimum weight, which means boundary processing is troublesome.

2. after reading the subscript process, we use the update method, that is, each time we add an item, you can use F [I] [J] to update f [I + A [k] [J + B [K], if I + A [k] Or J + B [k] exceeds the boundary, it is better to calculate the update boundary. It is easy to write.

 

Question 3:

The N-M edge of an undirected graph is given, and a path from S to T is obtained, so that the edge weights and the minimum values after the maximum edge weights are removed. Returns the sum of the minimum values. N <= 200, m <= 300

 

Problem solving process:

1. when we see such a small data range, we can enumerate the largest edge and perform Dijkstra Based on S and T respectively. Then there are two possibilities: Set the two ends of the largest edge to x, y, the path is either S-> X-> Y-> T, or S-> Y-> X-> T. In either case, obtain the updated answer with a smaller weight. This is a simple question.

 

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.