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. Echart 地图实例

    1.地图实例 function func_InEchart() { require.config({ paths: { echarts: '../Scripts/echart2/echarts' }, ...

  2. python UI自动化之切换iframe

    python+selenium写UI自动化的时候,经常会遇到需要切换iframe的情况,这里介绍几种切换iframe的方式 1.使用id定位 driver.switch_to.frame(" ...

  3. chrome headless+selenium+python+(ubuntu 16.04/centos7) 下的实现

    Ubuntu 16.04 下: 0x01 安装chrome 1 下载源加入系统源列表 sudo wget http://www.linuxidc.com/files/repo/google-chrom ...

  4. 如何开通linux机器的对外访问端口

    1.先查看是否已经开通 2.没有开通,去linux机器查看防火墙,确实没有开通 3.修改防火墙 vim /etc/sysconfig/iptables 4.重启防火墙之后重新查看已经可以看到8000端 ...

  5. 解决VS2017中出现:This function or variable may be unsafe

    解决办法:项目名称-右键属性-C/C++ - 预处理器 -预处理器定义 - 右侧下拉框中选择"编辑"- 在第一个编辑框中添加_CRT_SECURE_NO_WARNINGS

  6. Model&Form&ModelForm拾遗

    Model&Form&ModelForm拾遗 一.Model&Form&ModelForm Model:用于用户请求数据的验证(针对性弱),但有强大的数据库操作 For ...

  7. Ubuntu 安装 k8s 三驾马车 kubelet kubeadm kubectl

    Ubuntu 版本是 18.04 ,用的是阿里云服务器,记录一下自己实际安装过程的操作步骤. 安装 docker 安装所需的软件 apt-get update apt-get install -y a ...

  8. Ugly Number Gym - 101875B (最小表示法)

    题意:给你一串长度为n的数,这个数可以将后面的数挪到前面来,如果没有小于最开始的那个数的话就输出YES,否则输出NO 题解:如果后面有数字小于第一个数的话就肯定是NO了,这题的坑点就是如果前面很长一串 ...

  9. 解决Hbase启动后,hmaster会在几秒钟后自动关闭(停掉)!!!

    在日志(身为小白白的我,一开始日志在哪我都不知道!路径:/usr/local/hadoop/app/hbase-0.98.8/logs/hbase-hadoop-master-Master.log(也 ...

  10. AcWing 860. 染色法判定二分图

    #include <cstring> #include <iostream> #include <algorithm> using namespace std; , ...