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. CentOS7.5下基于FTP服务的局域网yum源搭建

    实验环境 yum源服务器和yum客户端都处于同一局域网之中 yum源服务器 hostname:zabbix OS版本:CentOS 7.5 IP:192.168.101.201 yum客户端 host ...

  2. 排队看病(PriorityQueue自定义排序)

    看病要排队这个是地球人都知道的常识. 不过经过细心的0068的观察,他发现了医院里排队还是有讲究的.0068所去的医院有三个医生(汗,这么少)同时看病.而看病的人病情有轻重,所以不能根据简单的先来先服 ...

  3. quartus 9.0 对话框显示不完整

    今天在quartus9.0新建SOPC build时,发现在添加PLL时对话框太大,笔记本分辨率差,导致结束和next不能选择: 解决: 如果电脑分辨率可以调整为合适的,就去调整: 现在介绍分辨率不合 ...

  4. Jquery拖拽,拖动排序插件

    上github搜jquery-sortable <!-- jq拖拽排序 --> <script src="${contextPath}/static/excelTable/ ...

  5. nuxt导入css样式

    全局导入,适用于所有组件 在nuxt.config.js文件引 css:["~样式path"], 如:css:["~assets/css/main.css"], ...

  6. selenium的鼠标事件操作

    自动化测试过程中,经常会用到鼠标事件,在selenium的action_chains模块的ActionChains定义了鼠标操作的一些事件,要使用ActionChains类中的方法,首先需要对Acti ...

  7. IE的css hack

    #element { color:orange; } #element { *color: white; } /* IE6+7, doesn’t work in IE8/9 as IE7 */ #el ...

  8. 5G手机来了,但这些问题让其短期内难以成为“香饽饽”

    在5G手机喊了太长时间后,其终于在近日不断亮相. 此前据中国质量认证中心官网显示,共有8款5G手机获得3C认证--华为4款,一加.中兴.OPPO和vivo各有一款.随着首批5G手机通过3C认证,意味着 ...

  9. Django的安装、使用详解、自动化测试应用以及程序打包

    1.Django的安装 pip install Django 验证 Django 是否能被 Python 识别 >>> import django >>> prin ...

  10. lminus

    lminus是Synopsy自带的tcl list 操作command. 顾名思义,可以将两个list相减,即过滤掉两个list中相同的element,生成一个新的list,其实是用lsearch与l ...