POJ3258-River hopscotch

Source: Internet
Author: User

Reprinted please indicate the source: Thank you

Http://user.qzone.qq.com/289065406/blog/1301820293

 

General question:

The length of a river is L. the start and end of the river have two stones respectively, and the distance from S to E is L.

There are n stones in the river, and each stone has a unique distance to S.

Ask if you want to remove M stones (except s and e). Each time you remove the stones associated with the current shortest distance, you must remove M stones, make the shortest distance at that time as big as possible and output the shortest distance.

 

Solution:

The second point of the classical theory is that it is not difficult to understand the meaning of the question (in fact, programming is not difficult, it is very difficult to understand ....)

View my detailsProgramFor more information, see my poj3273 practice. It looks different. The idea is similar. It's hard to understand all the mathematical problems. Be patient ....

I felt like I did not say it \ (^ o ^ )/~ Let's look at the program.

 

 

 

 

// Memory time // 420 K 391 MS # include <iostream> # include <algorithm> using namespace STD; int main (void) {int L; // river length int N; // Number of stones in the river (except starting point S and ending point E) int m; // Number of stones removed while (CIN> L> N> m) {/* input & initial */int * Dist = new int [n + 2]; // The distance from rock I to the starting stone is Dist [I] Dist [0] = 0; // The starting point sdist [n + 1] = L; // end point Eint low = L; // upper bound (the shortest distance of a hop) int high = L; // lower bound (the maximum distance of a hop) for (INT I = 1; I <= n + 1; I ++) {if (I <= N) // enter only 1 ~ N, when I = n + 1 is used only for finding lowcin> Dist [I]; If (low> Dist [I]-Dist [I-1]) low = DIST [I]-Dist [I-1];} Sort (Dist, DIST + (n + 2 )); // sort the stone to S in ascending order/* Binary-search */while (low <= high) {int mid = (low + high)/2; // compromise the distance between the maximum hop and the minimum hop, and check whether the mid is too large or too small relative to the optimal solution in binary search. // assume that the mid is the shortest distance after removing M stones. Int delrock = 0; // The number of stones that can be removed using the current mid value int sum = 0; // compared with poj 3273, here is the cumulative value of the continuous distance // when the first distance is accumulated, sumfor (INT I = 1; I <= n + 1 ;) {If (sum + = DIST [I]-Dist [I-1]) <= mid) {I ++; delrock ++ ;} else // when I + k Distance is accumulated from the I distance, if sum> mid, K distance is used as a segment {I ++; sum = 0; // set sum to 0 and accumulate from the distance I + k + 1} If (delrock <= m) // one of the difficulties in this question: even if delrock = m, it does not necessarily find the optimal solution low = Mid + 1; // The number of stones removed with the current mid value is smaller than the specified number, it indicates that the mid is too small. Else high = mid-1; // reverse mid is too large}/* output & relax */cout <low <Endl; Delete Dist;} return 0 ;}

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.