A. Three Friends

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Three friends are going to meet each other. Initially, the first friend stays at the position x=ax=a, the second friend stays at the position x=bx=b and the third friend stays at the position x=cx=c on the coordinate axis OxOx.

In one minute each friend independently from other friends can change the position xx by 11 to the left or by 11 to the right (i.e. set x:=x−1x:=x−1 or x:=x+1x:=x+1) or even don't change it.

Let's introduce the total pairwise distance — the sum of distances between each pair of friends. Let a′a′, b′b′ and c′c′ be the final positions of the first, the second and the third friend, correspondingly. Then the total pairwise distance is |a′−b′|+|a′−c′|+|b′−c′||a′−b′|+|a′−c′|+|b′−c′|, where |x||x| is the absolute value of xx.

Friends are interested in the minimum total pairwise distance they can reach if they will move optimally. Each friend will move no more than once. So, more formally, they want to know the minimum total pairwise distance they can reach after one minute.

You have to answer qq independent test cases.

Input

The first line of the input contains one integer qq (1≤q≤10001≤q≤1000) — the number of test cases.

The next qq lines describe test cases. The ii-th test case is given as three integers a,ba,b and cc (1≤a,b,c≤1091≤a,b,c≤109) — initial positions of the first, second and third friend correspondingly. The positions of friends can be equal.

Output

For each test case print the answer on it — the minimum total pairwise distance (the minimum sum of distances between each pair of friends) if friends change their positions optimally. Each friend will move no more than once. So, more formally, you have to find the minimum total pairwise distance they can reach after one minute.

Example

input

Copy

8
3 3 4
10 20 30
5 5 5
2 4 3
1 1000000000 1000000000
1 1000000000 999999999
3 2 5
3 2 6

output

Copy

0
36
0
0
1999999994
1999999994
2
4

解题说明:此题其实就是对三个数排序,然后缩短最大数与最小数之间距离。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include<iostream>
#include<algorithm>
#include <bits/stdc++.h>
using namespace std;

int main()
{
	long long a, b, c, n, i, t;
	scanf("%lld", &n);
	for (i = 1; i <= n; i++)
	{
		scanf("%lld%lld%lld", &a, &b, &c);
		if (a > b) t = a, a = b, b = t;
		if (b > c) t = b, b = c, c = t;
		if (a > b) t = a, a = b, b = t;

		if (a == b && b == c)
		{
			printf("0\n");
		}
		else
		{
			a += 1;
			c -= 1;
			if (c - a >= 0)
			{
				printf("%lld\n", 2 * (c - a));
			}
			else
			{
				printf("0\n");
			}

		}
	}
}
发布了1729 篇原创文章 · 获赞 371 · 访问量 273万+

猜你喜欢

转载自blog.csdn.net/jj12345jj198999/article/details/103749165