First, we need to clarify that the O (n) algorithm is required when the data size reaches 1 million.
How to Implement the O (n) algorithm is actually an improvement to the original algorithm.
The latter said that the original algorithm + a small property = O (n) Algorithm
Here are some examples to illustrate this:
1. Method for Finding the height array in the suffix array, brute-force algorithm hash + binary
With Height [rank [I]> = height [rank [I-1]-1 can do O (N)
2. noi2014 Zoo
Original algorithm: Jump back from each vertex until the condition is met. In the worst case, O (N ^ 2)
New Algorithm: using a clear nature, the F value of a prefix cannot be greater than its successor F value
I have never considered building a tree, and have a violent jump to a leaf node. For a non-leaf node, let its initial F value be the smallest of the successor F
We only need to run the DFS command from the root node once.
(PS: MS may also be stuck? However, the test results are quite good)
3. ch round #45 energy release
Original algorithm: Calculate the jurisdiction of each vertex in a monotonous queue, and then use rmq to calculate the complexity O (nlogn)
New Algorithm: Exploitation nature: If J is governed by I, then J's jurisdiction I will certainly be able to govern
In this way, we consider scanning the series along and against each other.
From left to right, the calculation of L [I] First makes J = I-1 if J is under I jurisdiction, j = L [J]-1, while updating the ans of this point, it is difficult to estimate the complexity until it cannot be jumped.
............
In general, to construct an O (n) algorithm, we need to make full use of the previously calculated items. Let's take a look at what obvious properties can be used,
What is the relationship between the calculation of this vertex and its pre-and post-calculation? Is it necessary to calculate each vertex from the beginning...
Continuous update ,,,