Three Friends

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

In one minute each friend independently from other friends can change the position x by 1 to the left or by 1 to the right (set x=x−1or x=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′ , b′ and 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′|, where |x|is the absolute value of x.

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≤1000) — the number of test cases.

The next qq lines describe test cases. The i -th test case is given as three integers a,b and cc (1≤a,b,c≤1e9,1≤a,b,c≤1e9 ) — 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.

题目大意:改题目就是说有三个朋友分别在x=a,x=b,x=c三点上,每一个朋友都可以向左或者向右移动一步或者选择不移动。

求移动(或者不移动)后三者最小的距离(|a′−b′|+|a′−c′|+|b′−c′|)

解析:(1)如果三者在一个点上就输出0,否则就给他们排序

      (2)排序后如果三者有两者相同算出其距离,如果距离<=2说明可以变 成0(如1 1 2)否者就sum-=4(如1 1 10可以让1+1 1+1 10-1)

      (3)如果以上都不是的话就(让最小的++,最大的--)在算sum;

AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
inline int read() {int x=,f=;char c=getchar();while(c!='-'&&(c<''||c>''))c=getchar();if(c=='-')f=-,c=getchar();while(c>=''&&c<='')x=x*+c-'',c=getchar();return f*x;}
typedef long long ll;
const int maxn = 1e6+;
int a[];
int main()
{
int t;
cin>>t;
while(t--){
for(int i=;i<;i++){
cin>>a[i];
}
if(a[]==a[]&&a[]==a[]){
printf("0\n");
}
else{
sort(a,a+);
ll sum=;
if(a[]==a[]||a[]==a[]){
sum+=abs(a[]-a[])+abs(a[]-a[])+abs(a[]-a[]);
if(sum<=){
sum=;
}
else{
sum-=;
}
}
else{
a[]++;
a[]--;
sum+=abs(a[]-a[])+abs(a[]-a[])+abs(a[]-a[]);
}
printf("%d\n",sum);
}
}
return ;
}

  

    

 

最新文章

  1. null和undefined的一些区别
  2. 美团(iPad)顶部界面的简单实现, 及开发时常见bug
  3. 指令重排序及Happens-before法则随笔
  4. Java中浮点数的基础知识
  5. [LeetCode] Interleaving String 解题思路
  6. 使用Web Application Stress Tool 进行压力测试
  7. Android智能手机屏蔽电话与屏蔽安装软件功能
  8. crontab的两大坑:百分号和环境变量
  9. tps 和 qps的区别
  10. Quartz简答介绍
  11. opencv: flip函数的使用;
  12. HDU 2888 Check Corners (模板题)【二维RMQ】
  13. 挖掘两个Integer对象的swap的内幕
  14. JMeter 生成精度度为分钟的时间戳文件名
  15. centos7 如何在用户级对资源进行限制
  16. 最干净,最便捷的卸载Mysql
  17. iOS 编译部署路径
  18. User Agent 里的各个字段含义
  19. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed(在64位模式下运行安装了32位的Oracle客户端组件时,会发生此问题)
  20. JavaWeb基础—CSS学习小结

热门文章

  1. C++-指针阅读能力提升
  2. django 搭建一个投票类网站(一)
  3. POJ3273 Monthly Expense (二分最小化花费)
  4. layui下select禁止点击
  5. Myeclipse的一些快捷键整理(转)
  6. Web API入参,响应规范
  7. CAN总线冲突裁决
  8. 「模板」Splay
  9. 题解【洛谷P1407】 [国家集训队]稳定婚姻
  10. [USACO08JAN]Haybale Guessing(LuoguP2898)