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 ;
}

  

    

 

codeforces-Three Friends的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

  10. CodeForces - 453A Little Pony and Expected Maximum

    http://codeforces.com/problemset/problem/453/A 题目大意: 给定一个m面的筛子,求掷n次后,得到的最大的点数的期望 题解 设f[i]表示掷出 <= ...

随机推荐

  1. 《TCP/IP入门经典》摘录--Part 1

    TCP/IP基础知识 什么是TCP/IP Transmission Control Protocol/Internet Protocol的简写,中译名为传输控制协议/因特网互联协议,又名网络通讯协议, ...

  2. [NOI2010] 超级钢琴 - 贪心,堆,ST表

    这也算是第K大问题的套路题了(虽然我一开始还想了个假算法),大体想法就是先弄出最优的一批解,然后每次从中提出一个最优解并转移到一个次优解,用优先队列维护这个过程即可. 类似的问题很多,放在序列上的,放 ...

  3. 11g RAC添加用户表空间(数据文件)至文件系统(File System)的修正

    前提:非TEMP.UNDO和SYSTEM表空间,这仨是大爷,您得搂着点.来自博客园AskScuti .客户是添加临时表空间数据文件时,不小心 ADD 到了文件系统中,然后发现,后悔了,还在OS层面 R ...

  4. C#获取当前不同网卡对应的iP

    C#获取当前不同网卡对应的iP: public string GetLocalIP() { IPAddress localIp = null; try { IPAddress[] ipArray; i ...

  5. HIT大作业——hello的一生

    hello的一生 关键词:计算机系统:功能:流程:P2P;O2O;hello                              目  录   第1章 概述- 4 - 1.1 Hello简介 - ...

  6. RS-232C

    RS-232C标准(协议)的全称是EIA-RS-232C标准,定义是"数据终端设备(DTE)和数据通讯设备(DCE)之间串行二进制数据交换接口技术标准".它是在1970年由美国电子 ...

  7. crontab实践

    1.crontab概要 2.crontab使用 3.关键配置信息 3.1如何配置定时任务 4.注意事项 参考 https://www.cnblogs.com/keithtt/p/6946498.htm ...

  8. 百炼OJ - 1007 - DNA排序

    题目链接:http://bailian.openjudge.cn/practice/1007 #include<stdio.h> #include<algorithm> usi ...

  9. sqli-libs(29(jspstudy)-31关)

    Less_29 Less-29: 需要用到jspstudy跟phpstudy   搭建jspstudy: sqli-labs-master文件夹下面还有tomcat文件,这才是真正的关卡,里面的jsp ...

  10. 随机获取list或set或map中的一个元素

    转自:https://m.2cto.com/kf/201507/412937.html import java.util.HashSet;import java.util.List;import ja ...