Summary of two points

Source: Internet
Author: User

Noip two points should be a very simple algorithm, last year Noip Day2-t1 is bare two points, here are two examples

1, Poj2456:farmer John have built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls is located along a straight line at positions X1,..., XN (0 <= XI <= 1,000,000,000). His C (2 <= C <= N) cows don ' t like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting all other, FJ want to assign the cows to the stalls, such that the minimum distance bet Ween any and them is as large as possible. What is the largest minimum distance? (to the extent that there are cows and barns, we try to maximize the distance between the nearest two cows)

2, Jumping Stone (NOIP2015-DAY2-T1): Every year the cows have a special version of the Jumping House competition, including jumping from one rock to another in the river. This exciting activity is carried out in a long, straight channel with a rock at the end of the starting point and from the beginning L far (1≤l ≤1,000,000,000). Between the starting and ending points, there are n (0≤di (0 < di < l ). during the race, the cows take turns from the starting point, trying to reach the finish line, each step can only jump from one rock to another rock. Of course, the weak cows have no way of accomplishing their goals. Farmer John was proud of his cows and watched the game every year. But as time went by, he was bored to watch the other farmer's timid cows move slowly between the rocks that were very close. He plans to remove some rocks so that the shortest jumping distance from the starting point to the end point is the longest. He can remove up to m (0≤m n ) a rock. Please help John to determine the maximum possible minimum jumping distance after removing these rocks. (Simply to minimize the maximum value)

This type of problem has a distinct characteristic: to maximize the minimum or to minimize the maximum value. At this point, the approximate step is to sort the data that is being read (because the binary lookup is done on the ordered data) and then executes the two points. The check function in two points is the most critical part, for example, in the first question we are the two answers, and then in check in the greedy to arrange the position of the cow, to see if the answer can be met. The second question is also the two-point answer, check in accordance with the answer we find the answer to not meet the stone removed, to see if the answer can be met. This is the approximate idea of check: to see if the answer is met.

There is also an important detail in the two points: mid= (l+r)/2,l=mid+1,r=mid when the minimum value is found, and mid= (l+r+1)/2,l=mid,r=mid-1 when looking for the maximum value of the minimum value. Otherwise the program will get into a dead loop or get the correct answer.

The last point is when the real number is two points because the precision needs often we will let the program continue to calculate, but due to the accuracy of the calculation error, often also makes the program inefficient or dead cycle. Generally speaking, we can limit the number of times, generally 60-70 times to meet the accuracy needs, so as to improve program efficiency, to avoid the situation of tle.

The last point, this kind of problem data volume is generally very large, remember to read in with scanf, the first question I because with CIN and tle, changed to scanf immediately after.

Summary of two points

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.