Steps ultraviolet 846

Source: Internet
Author: User

Description: This question requires finding the shortest step from integer x to integer y, and the first and last steps must be one. At the same time, each step is one step more than the previous one, one step or the same. If you want to figure out how each step is taken in detail, this question is quite troublesome. Considering the maximum deviation between the two steps, each number between one and the maximum numbers must appear from left to right and from right to left. We can anticipate such a series.

1, 2, 3,... N-2, n-1, N, n-1, N-2... 3, 2, 1, if the sum of the number of columns is less than Y and X is certainly not good. So we need to adjust n to the difference that is equal to or greater than Y and X. Obviously, when the difference between Y and X happens to be the sum of the series, the result is directly output. Otherwise, the maximum number of columns that constitute the Y and X differences must be n-1. At this time, because the number of steps is the least, we start from N-1 and try to add as many places as possible. After the difference of Y-X is finally placed, we can obtain the minimum number of steps. For example, if y-x = 7, we can find that 1, 2, 3, 2, 1 is greater than 9, then the sequence 1, 2, 1 must exist in the 8 series, in this case, 7-4 = 4. A maximum of 2 values can be placed. There is only one left, and you can enter it.


Question:

Steps

One steps through Integer Points of the straight line. The length of a step must be nonnegative and can be by one bigger than, equal to, or

Now you have to take a few steps from a point in the number axis to another point. The length of each step is non-negative and can be greater than the previous step, equal or smaller.

By one smaller than the length of the previous step.

What is the minimum number of steps in order to get fromXToY? The length of the first and the last step must be 1.

What is the minimum number of steps from X to Y? The length of the first and last steps must be 1.

Input and outputinput consists of a line containing N, the number of test cases. For each test case, a line follows with two integers: 0 X Y<231.

The input row contains an integer N, which is the number of groups for the trial. For each group of Token test data, a row has two integers: 0.XY<231. For each row of shard Test Data

Each test case, print a line giving the minimum number of steps to get fromXToY.

The output line shows the minimum number of steps from X to Y.

Sample Input
345 4845 4945 50

Sample output
334

Source code:

# Include <stdio. h> int n, x, y, Q; int main () {int I, J, K, result; // freopen ("input.txt", "r", stdin ); scanf ("% d", & N); While (n --) {scanf ("% d", & X, & Y); q = Y-X; if (q <= 3) {// In special cases smaller than three, printf ("% d \ n", q); Continue ;}for (I = 2 ;; I ++) if (I * I> = q) break; if (I * I = q) {printf ("% d \ n", 2 * i-1 ); continue;} else I --; q-= I * I; Result = 2 * I-1; while (q) {// start from the maximum number of columns, result + = Q/I; q = Q-I * (Q/I); I --;} printf ("% d \ n", result );} return 0 ;}











Steps ultraviolet 846

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.