Application of greedy algorithm in the competition

xiaoxiao2021-03-06  87

Original: hal burch translation:

Anger

The translator's words:

Hal Burch has drawn an amazing discovery in the spring of 1999, in fact, there is only 16 kinds of competition test types. In IOI, the first few have a problem of about 80%. The greedy algorithm (also translated as a greedde algorithm) is one of the "first few".

When a problem has the best child structure, we will think of it with a dynamic planning method, but some problems have a simpler and effective way, we only need to make the best choice to appear. The choice made by the greedy algorithm can rely on the choice created in the past, but never rely on future choices, nor depends on the solution of sub-problem, which makes the algorithm have a certain speed advantage in the process of encoding and executing. If a problem can be solved simultaneously, the greedy algorithm should be one of the best choices.

With the "Pixel" tool, we can build a more general theory about greedy algorithms, but in this case, more need to be personal experience to judge when to use greedy algorithms. Because the greedy algorithm is not to get the overall optimal solution or the most ideal approximate solution, the applicable area is relatively narrow, so it is very important to determine its application timing. In short, the greedy algorithm is not deep, but it can solve many seemingly deepest problems (such as Dijkstra just passing Dijkstra on the shortest path problem in single source), this article translated by the translator is a foreign player Some of this experience should say that he is not very deep, but he shows us how to use the popular thinking analysis of the main issues, correctness, timing and skills of the greed, I want to have respective levels. Readers will have a certain degree of help.

how are you

I. Example: Barn repair [1999 USACO Spring Open]

There is a long column owner, some of which need to be covered with wooden boards. You can use up to N (1 <= n <= 50) wood board, each of which can override any number of continuous colons. Covers all the colors that need to be covered, but make the covered columns as little as possible. Thought:

The basic thinking behind the greedy algorithm is to promote from a small solution to a large solution. However, different from other methods is that the greedy algorithm only needs to maintain the best solution with the process. Therefore, for this example, if you need to find the optimal solution when n = 5 is required, you should look for the optimal solution at N = 4, and then change the solution to N = 5. As for other solutions at N = 4, it may not be considered. The greedy algorithm quickly, and only small extra memory consumption is required. But very unfortunately, it is often incorrect. However, when they are indeed correct, it can be easily implemented and has enough fast speed. Question:

There are two basic problems in the greedy algorithm. How to build:

How to launch a large solution from a smaller solution? In this case, the most obvious way to change from four wooden boards to five wooden boards is to remove a wooden board to two. You should choose the maximum removed only the part that doesn't have to be overwritten. In order to remove such a part, select a piece of wooden board spanning this part into two parts, a portion of the previous corral before, and the other covers the part after these corrals. Is the solution true?

For programmers, the real challenge is that the greedy algorithm does not necessarily have this fact. Even if it is a specific input, random input, or everything you can think of is correct, but if there is any case, it cannot work correctly, at least one (or more) referee test will be this type. For this example, in order to ensure that the greedy algorithm is really effective, it should be made as follows: Assuming the answer does not include a large gap that the greedy algorithm removes, but includes a small gap, by combining the wooden boards at both ends of the smaller gap And blow open the answer to the big gap, use and the original as much wooden board, but there are fewer colors. This new program is better, so the previous assumption is wrong, we should always choose to remove the largest gap. If the answer does not contain this specific gap but contains a gap as it is as big as it, the answer generated by the same approach is the same as the original on the covered column and the number of woods used. The new method is the same as the original method rather than more excellent, so we have to choose which one is ok. Therefore, there is a best answer to the big gap. Each step is true, and the optimal plan for further further results is the last supercharge. Therefore, the final solution is optimal. Conclusion: Use it if a greedy solution exists. It is easy to encode, easy to debug, run fast, and consume less memory is a good algorithm in the competition. The only lack of ingredients is correct. If the greedy algorithm finds the right answer, resolutely use it, but never think that the greedy algorithm is valid for all the problems. Second, the problem example: Sort by a sequence of three values ​​[IOI 1996]

There is a sequence containing three values ​​(1, 2, 3) whose length does not exceed 1000. Looking for a scenario to use the minimum number of times to sort the sequence. Sorted sequences are divided into three parts, a portion of 1, and a portion of 2 and a portion of 3. The greed of greed will exchange 1 in the 2 and 2 parts of the 1 part as much as possible, will exchange 1 in the 3 and 3 parts of the 3 and 3 as much as possible, 3 and 3 in the number of 2 parts as much as possible 2 switches in the section. Once this is no longer existed, the remaining elements do not need three three rotations in the position where it should be in. Enable all 1 to stand, then all 2 ends. Analysis:

Obviously, one exchange can maximize the two elements at the position they should, so all the first exchange is optimal. At the same time, they use different types of elements, so there is no conflict between these types. This means that the order is irrelevant. Once these exchanges are executed, the best choice you can do is brought to the elements that are not in their position, which is the task to be completed by the second part. (For example, if all 1 has been in the position thereof, there are some 2 in the 3 part, and there must be the same 3 in the 2 portion, and it is already possible to exchange. ) Third, an antithoxity: Friendly Coins (Direction)

There is a newly established country called The Dairy Republic, and the information you will receive includes the national coin value information and some of the money. Try out the number of money with the minimum number of coins. The dairy republic will definitely contain 1 point of coins. (Translator Note: This is an analysis of the full non-essential condition that guarantees a certain amount of money in all situations.

Obviously, you will never consider choosing a small-faced coin, because that means you have to take more quantity coins, so the greedy algorithm is feasible. Maybe not this: OK, this algorithm is usually effective. In fact, for the US coin system {1, 5, 10, 25}, it can always produce the optimal solution. However, for other systems, like {1, 5, 8, 10}, if the target is 13, using a greedy algorithm will take away 10 and three 1, a total of four coins. However, only two coins contain two coins are exist. Fourth, topology is now given a group of objects, which contain some order constraints, such as "A must be ranked in B", finding a order such that all constraints can be established. Algorithm:

Create a picture through these objects, if "A must be placed in front of B", there is an arc between A to B. Manufacture a path through these objects in any order. Whenever you find an object (internal score) of 0, you are greedily put it in the final order of the existing order, delete all the arcs that it as a starting point, and then do the same as the son object before it Check. If this algorithm does not make all objects pass through such sorting, there is no order to meet these constraints.

转载请注明原文地址:https://www.9cbs.com/read-107179.html

New Post(0)