O (n) Algorithm

Source: Internet
Author: User

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 ,,,

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.