blog.itcode.devblog.itcode.dev

[Baekjoon / JAVA] Baekjoon Algorithm #1011 Fly Me to the Alpha Centauri

As a child, Woohyun believed that a future would come where humanity could live on planets other than Earth. Now, 23 years after he first set foot on this world called Earth, he is the world's youngest ASNA astronaut, waiting for the glorious moment when he'll set foot on a new world. The spaceship he'll board carries a large-scale life support system to help settle Alpha Centauri, a new home for humanity, and because of its enormous size and mass, it's equipped with a space-jump device developed by mobilizing the latest technology. However, this space-jump device has the drawback of causing serious mechanical faults if the travel distance increases too sharply, so if it moved k light-years the previous time it was activated, it can only move k - 1, k, or k + 1 light-years the next time.

[Baekjoon / JAVA] Baekjoon Algorithm #1011 Fly Me to the Alpha Centauri

As a child, Woohyun believed that a future would come where humanity could live on planets other than Earth. Now, 23 years after he first set foot on this world called Earth, he is the world's youngest ASNA astronaut, waiting for the glorious moment when he'll set foot on a new world. The spaceship he'll board carries a large-scale life support system to help settle Alpha Centauri, a new home for humanity, and because of its enormous size and mass, it's equipped with a space-jump device developed by mobilizing the latest technology. However, this space-jump device has the drawback of causing serious mechanical faults if the travel distance increases too sharply, so if it moved k light-years the previous time it was activated, it can only move k - 1, k, or k + 1 light-years the next time.
RWB0104
@RWBwritten at 2021-06-11 05:14:09
Baekjoon Algorithm

시리즈 모아보기

Baekjoon Algorithm

13 / 22
RankLanguage Used

🖼️ JAVA

🔗 Problem #1011

Time LimitMemory Limit
2 sec512MB

As a child, Woohyun believed that a future would come where humanity could live on planets other than Earth. Now, 23 years after he first set foot on this world called Earth, he is the world's youngest ASNA astronaut, waiting for the glorious moment when he'll set foot on a new world.

The spaceship he'll board carries a large-scale life support system to help settle Alpha Centauri, a new home for humanity, and because of its enormous size and mass, it's equipped with a space-jump device developed by mobilizing the latest technology. However, this space-jump device has the drawback of causing serious mechanical faults if the travel distance increases too sharply, so if it moved kk light-years the previous time it was activated, it can only move k1k - 1, kk, or k+1k + 1 light-years the next time. For example, the first time this device is activated, it could in theory move -1, 0, or 1 light-years, but since moving a negative or zero distance is meaningless, it can effectively move 1 light-year, and the next time it can move 0, 1, or 2 light-years. (If it then moves 2 light-years again, the time after that it can move 1, 2, or 3 light-years)

Since Woohyun Kim knows well how much energy the space-jump device consumes each time it's activated, he wants to travel from point xx to point yy using the minimum number of activations. However, for the safety of the space-jump device, he wants the movement distance right before arriving at point yy to always be exactly 1 light-year.

Write a program for Woohyun Kim that computes the minimum number of space-jump device activations required to travel exactly from point xx to point yy.

The first line of the input gives the number of test cases TT. For each test case, the current position xx and the target position yy are given as integers, with xx always smaller than yy. (0x<y<231)(0 ≤ x < y < 2^31)

For each test case, print the minimum number of space-jump device activations required to travel exactly from point xx to point yy.

  • Input

TC

3
0 3
1 5
45 50
  • Output

TC

3
3
4

The title seems to be a homage to Frank Sinatra's Fly me to the moon.

I was first introduced to Sinatra through Blue Moon in Fallout: New Vegas, and afterward I found myself listening to him often, since there are so many great tracks like Come Fly With Me and Theme from New York, New York.

Back to the problem — at a glance you might think, "why not just go as fast as possible?" But the following two conditions get in the way.

  1. At the very first and very last segments, you must always jump exactly one square.
  2. If you moved kk distance, you can only move k1k - 1 to k+1k + 1 next.
  3. You must always land exactly on the target point (passing over it doesn't count).

Because of these conditions, you can't just wander around aimlessly, so to speak.

Since this isn't about counting possibilities but about following a fixed rule, calculating and listing the results in order should help us find a clue.

DistSequenceActivations
111
21 12
31 1 13
41 2 13
51 2 1 14
61 2 2 14
71 2 2 1 15
81 2 2 2 15
91 2 3 2 15
101 2 3 2 1 16
111 2 3 2 2 16
121 2 3 3 2 16
131 2 3 3 2 1 17
141 2 3 3 2 2 17
151 2 3 3 3 2 17
161 2 3 4 3 2 17

It may not be obvious at a glance, but when looking for a pattern, the most convenient reference points are perfect squares (1, 4, 9...). The characteristics are as follows.

  • The activation count increases by 1 right after a perfect square.
  • The activation count increases by 1 at the midpoint between the current perfect square and the next.

That is, there's a visible change right after each perfect square, and the activation count increases by 1 at the midpoint of the interval based around a perfect square.

DistSequenceActivations
111
41 2 13
91 2 3 2 15
161 2 3 4 3 2 17

The activation count for a perfect square follows this pattern. The general formula for the activation count of a perfect square nn is as follows.

2n12\sqrt{n} - 1

For 9, 231=52 * 3 - 1 = 5, so we can confirm the formula holds.

Since the activation count changes at the midpoint between perfect squares, we just need to calculate this midpoint. Suppose we have a general, non-perfect-square number kk. Since this pattern revolves around perfect squares, we need to derive the relevant perfect square from kk. We need to find:

  • The smallest perfect square greater than kk
  • The midpoint of the perfect-square interval that kk falls into
  1. Take the square root of kk and round it. This gives us the square root n\sqrt{n} of the perfect square nn that's greater than kk and closest to it.
  2. Square n\sqrt{n} to get the nearest perfect square nn.
  3. Compute the midpoint tt of the perfect-square interval containing kk, using nnn - \sqrt{n}.
  4. If k>tk > t, apply the formula 2n12\sqrt{n} - 1, the same as the activation count for nn.
  5. If k<=tk <= t, apply the formula 2n22\sqrt{n} - 2, which is the activation count for nn minus 1.

Let's use this method to compute the activation count for 7.

Since 72.646\sqrt{7} \fallingdotseq 2.646, rounding this gives 3. That is, the smallest perfect square greater than 7 is 32=93^2 = 9.

Since 99=93=69 - \sqrt{9} = 9 - 3 = 6, the midpoint of the perfect-square interval containing kk is 6. If the number is greater than 6, its activation count matches that of 9. Since the given number is 7, its activation count matches that of 9.

The activation count of 9 is 291=61=52\sqrt{9} - 1 = 6 - 1 = 5, so the activation count of 7 is also 5.

We just need to translate the above procedure into code. The implementation difficulty is low.

JAVA

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Baekjoon problem #1011 algorithm class
 *
 * @author RWB
 * @see <a href="https://blog.itcode.dev/posts/2021/06/11/a1011">1011 solution</a>
 * @since 2021.06.11 Fri 09:06:34
 */
public class Main
{
	/**
	 * Main function
	 *
	 * @param args: [String[]] arguments
	 *
	 * @throws IOException data input/output exception
	 */
	public static void main(String[] args) throws IOException
	{
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		
		// Number of cases
		int T = Integer.parseInt(reader.readLine());
		
		for (int i = 0; i < T; i++)
		{
			String[] temp = reader.readLine().split(" ");
			
			// Current position
			double x = Double.parseDouble(temp[0]);
			
			// Target position
			double y = Double.parseDouble(temp[1]);
			
			// Distance between x and y
			double distance = y - x;
			
			System.out.println(solve(distance));
		}
		
		reader.close();
	}
	
	/**
	 * Function that returns the number of activations
	 *
	 * @param distance: [double] distance
	 *
	 * @return [int] number of activations
	 */
	private static int solve(double distance)
	{
		int result;
		
		double ref = Math.sqrt(distance);
		
		// If it's a perfect square
		if (ref % 1 == 0)
		{
			result = (int) (2 * ref - 1);
		}
		
		// Otherwise
		else
		{
			double next = Math.ceil(ref);
			
			// If it's greater than the midpoint between the previous and next perfect squares
			if (distance > Math.pow(next, 2) - next)
			{
				result = (int) (2 * next - 1);
			}
			
			// Otherwise
			else
			{
				result = (int) (2 * next - 2);
			}
		}
		
		return result;
	}
}

One thing to be careful about: the maximum value of xx and yy is 2312^31. The maximum value of int is 2,147,483,647, but 2312^31 is 2,147,483,648, so you must not use int when computing the distance between xx and yy. Only the final result should be printed as an int.

I considered applying memoization, but since we'd need to initialize an array of size 2312^31, that would actually cause even worse overhead. Since this isn't a recursive function either, memoization probably wouldn't make much of a difference.

  • Math
# Baekjoon# Algorithm# JAVA(Java)# SILVER# SILVER I
ship
blog.itcode.dev

Notes from the π-th Alpaca

7.0.1
Developed by RWB since 2021.057th upgraded at 2026.08