Zoj 1871. Steps

Source: Internet
Author: User

Zoj 1871: Steps

Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 871

Assume that a digital scale is marked on a straight line. The step size of each step cannot be negative, and the relationship with the previous step size must be equal or greater than or less than 1. The step size of the first and last steps must be 1. The minimum number of steps from X to Y.

 

You can use mathematical language to re-Describe the following questions: Give an unsigned integer d (= Y-x), which can be divided into the sum of n positive integers that meet the following conditions, and obtain the minimum value of N:

D = Sigma Si; (I = 1... n)

And meet the following requirements:

S1 = Sn = 1;

| S (I + 1)-Si | ≤ 1;

 

Analysis: according to the requirements of the question, in an ideal condition, in order to reach the minimum number of steps, the step size change should be increased from 1, 2, 3... to the maximum m, and then gradually reduced to 1:

Distance = Y-X;

Distance = 1 + 2 + 3 +... + M +... + 3 + 2 + 1 = m ^ 2;

Steps = 2 * m-1; (number of steps)

Therefore, if the distance is exactly the square of m, the number of steps must be (2-1), where M is the maximum step size. If the distance is not the number of degrees, assuming that the distance is between m ^ 2 and (m + 1) ^ 2

Distance = m ^ 2 + remain; (0 <remain <= 2 m)

Remain is the ending number of shards. If remain is smaller than or equal to m, we only need to insert a remain step into the previous sequence. If remain is greater than m, one step is not enough. We need to insert at least two steps, M and remain-M (of course, remain/2 and remain-remain/2 ). Because we cannot insert a step larger than m, otherwise the sum will eventually reach the square of the next vertex (m + 1 ).

 

Therefore, based on the above analysis,Code(M is represented by maxstep for readability in the Code ):

Code_zoj1871

# Include <stdio. h> # Include <Math. h> Int Main ( Int Argc, Char * Argv []) {unsigned Int X = 0 , Y = 0  ; Unsigned  Int  Distance, maxstep, remain, steps;  While (Scanf ( "  % Lu  " , & X, & Y )! = EOF) {distance = Y- X;  If (Distance = 0 ) {Printf (  "  0 \ n  "  );  Continue  ;} Maxstep = (Unsigned Int  ) SQRT (distance); remain = Distance-maxstep * Maxstep; Steps = Maxstep * 2 - 1  ;  If (Remain> 0  ){  If (Remain <= Maxstep) Steps ++ ;  Else  Steps + = 2  ;} Printf (  "  % Lu \ n  "  , Steps );}  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.