// UVa 846 - Steps
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
for (int cases = scanner.nextInt(); cases > 0; cases--) {
int from = scanner.nextInt(), to = scanner.nextInt();
System.out.println(neededSteps(to - from));
}
}
private static int neededSteps(int target) {
if (target == 0) return 0;
int x = (int) ((Math.sqrt(1 + 4 * target) - 1) / 2);
int leftover = target - x * (x + 1);
if (leftover == 0)
return 2 * x;
else if (leftover <= x + 1)
return 2 * x + 1;
else
return 2 * x + 2;
}
}
Monday, December 23, 2019
UVa 846 - Steps
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment