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. [ZJOI2014] 力 - 多项式乘法 FFT

    题意:给定 \({q_i}\),求 \[E_i = \sum_{i<j}{\frac{q_j}{(j-i)^2}} - \sum_{i>j}{\frac{q_j}{(j-i)^2}}\] ...

  2. 超大jtl解析jar包使用源于testfan

    解决超大jtl解析过慢时间过长问题: usage:  java  -jar  jmeter-graph.jar    jtl_path    interval_time (单位:s) 10G文件分析测 ...

  3. TCP/IP详解,卷1:协议--IP:网际协议

    引言 I P 是 T C P / I P 协议族中最为核心的协议.所有的 T C P.U D P.I C M P 及 I G M P 数据都以 I P 数据 报格式传输(见图 1 - 4).许多刚开始 ...

  4. C++-POJ1200-Crazy Search[hash]

    由于已经给出字符只有NC种,故可以把子串视为一个NC进制的数,以此构造hash函数就可以了 #include <set> #include <map> #include < ...

  5. 1.4 面试问题整理: ATM机取款

    流程图:

  6. set,get,setter

    JS对象属性中get/set与getter/setter是什么 2019-01-18 15:07:44 CHENKAI188 阅读数 686更多 分类专栏: JS修仙系列   版权声明:本文为博主原创 ...

  7. cookie不支持中文,必须转码后存储,否则会乱码

    cookie不支持中文,必须转码后存储,否则会乱码 Cookie ck = new Cookie("username", URLEncoder.encode(name, " ...

  8. JS实现点击table中任意元素选中

    上项目开发,忙的焦头烂额,博客也没咋更新了. 昨天老师提了个需求,简单的小例子,选择tr中任一行选中tr,觉得很有意思,记录一下: 上代码 <!DOCTYPE html> <html ...

  9. AJAX--XMLHttpRequest对象

    创建XMLHttpRequest对象 XMLHttpRequest是AJAX的基础. 所有现代浏览器(IE7+.Firefox.Chrome.Safari以及Opera)均内建XMLHttpReque ...

  10. [Note]后缀自动机

    后缀自动机 代码 #include <cstdio> #include <algorithm> #include <cstring> const int M = 1 ...