Programming beauty 4.7 | ant crawling

Source: Internet
Author: User

There is a wood pole with a length of L parallel to the X axis, and the X coordinate of its left endpoint is 0 (so the X coordinate of the right endpoint is L ). At the beginning, there were N ants on the top, and I (1 ≤ I ≤ n) the abscissa of only ants was Xi (assuming Xi has been arranged in ascending order ), the direction is di (0 indicates to the left, 1 indicates to the right), and each ant moves forward at the speed v. When any two ants meet, they both turn their heads in the opposite direction, the speed remains unchanged. Write a program to find the shortest time and longest time for all ants to leave the wooden pole.

Solution:

We assume that each ant carries a bag of food, and any two ants exchange their own food and turn their heads. In this case, each time an ant leaves the wooden pole, it means a bag of food leaves the wooden pole (although it may not be the bag it carried at the beginning ). Therefore, we can find the time for each bag of food to leave the wooden pole (because the food will not turn around ). Because the time when each bag of grain leaves the pole corresponds to the time when an ant leaves the pole, this is a one-to-one ing relationship. The time when all the ants leave the wooden pole is equal to the time when all the food leaves. The longest time is the maximum time when all the food leaves the farthest direction from one end. The shortest time, it is the minimum time for all food to go in the closest direction to one end.

Extension:

  1. When will the I-th ant financial from the left leave the wooden pole?
  2. How many times have all the ants collided with each other from the beginning?
  3. At which time does the k-th collision occur? Where? Between which two ants?
  4. Which ant has the most collisions?
  5. If it is not a wooden pole but an iron circle, will all the ants Return to the status after a period of time? What is the upper limit of this time?

Extension 1.

Proposition: if at the beginning m only went to the left, and n-m only went to the right, then M finally fell from the left of the pole, and n-m only fell from the right of the pole.

This proposition can easily prove that each collision does not change the number of ants to the left and right. As a result, each collision with the ant will turn its head rather than pass through, the first m of the first m from the left must eventually fall from the left, and the next n − m of the ant will fall from the right of the wooden pole. Because we know where each bag of food falls, therefore, the departure time of M bags of grain on the left corresponds to the departure time of M ants from the left, which is similar to that on the right. Therefore, we only need to judge the relationship between I and m to know whether the ant financial of I falls from the left or the right. Assume that it is falling from the left, so the ant's falling time is equal to the falling time of the I-bag from the left. Time complexity O (N), scan it to know the time when all the food falls, and use an array to save it. The first M stores the fallen food on the left, the next N-M pieces Save the fallen grain on the right. If I <= m, you can find the time of the I-th. If I> M, N-(I-m) is obtained directly.

Extension 2.

We only need to obtain the number of collisions of each ant, and then accumulate the number. Here we use another idea. If we regard collision headers as moving forward without turning them in, it is easy to see the original problem (number of collisions) the number of passes (because the speed does not change, the original collision corresponds to the current one ). For each ant to the left, it will only "pass" the ant to the right on its left. Therefore, the number of times each ant passes is equal to the number of ants that are in the opposite direction in its forward direction at the beginning. The time complexity is O (n ). As long as the Count of [0, I-1] in two directions, and then compare with the current direction of the ant can be in O (n) statistics.

Extension 3.

First, we assume that V is 0.5 characters in length per second. each ant is at the starting point, so that each collision occurs in the whole second. The advantage of this assumption is that we can divide the moment of the k-th collision. If the collision time is a floating point number, this binary point may never be terminated. We still think that every ant has a bag of food, so every bag of food is easy to master (that is, exchange from one ant to another), there is a collision. Because the direction of food is fixed, we can easily find all the easy-to-Master time for each bag of food in its "Forward" direction (the number of easy-to-Master times equals the number of "passes" times in expansion 2 ). In this way, our problem is equivalent:

Find the minimum time t, so that the frequency of the master is greater than or equal to K if the master is less than or equal to T.

Because all collision (easy-to-Master) Time is the whole point, we can use two TBS, and then use linear complexity to find the number of easy-to-Master times that are less than or equal to T. The entire complexity is O (n unknown log (| maxt − mint |). maxt and mint indicate the first and last collision time respectively, which can be obtained within O (n) time.

In the previous section, you still need some tips to use the linear time complexity to find the number of easy masters at a time point less than or equal to T. The following code uses an array pi to represent the initial position of the ant financial whose nth direction is right. When the ant financial whose nth direction is left is scanned, assume that the obtained mean value is I '(that is, the time when the food and the bag of food are easier to master from p0 to Pi are greater than T ). Because the time of the bag of food to the left is increasing, and the initial position of the next ant to the left is greater than the current (J to the left) Ant, therefore, for the next ant, the time at which P0 to Pi 'corresponds to the food and the time when ant carries the food must be greater than T. That is, the position of the Value Point is monotonous. Therefore, we can calculate the number of requests in the time of the average O (n.

While finding the moment, we also find the location, and the second question also solves the problem. The next step is to ask which two ants have this collision (if there are multiple collisions at the same time, find any one ). In fact, we only need to find the position of each bag of food at t time. Because each bag of food is bound to correspond to an ant holding it, we only need to sort the positions of these foods and find the grain with the same position and its subscript (from left to right ), then we found out the right ant.

From: http://lam8da.sinaapp.com /? P = 11

Programming beauty 4.7 | ant crawling

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.