CF 605 div3 A.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 (i.e. set x:=x−1 or 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 q independent test cases.
Input
The first line of the input contains one integer q (1≤q≤1000) — the number of test cases.
The next q lines describe test cases. The i-th test case is given as three integers a,b and c (1≤a,b,c≤109) — 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.
Example
input
8
3 3 4
10 20 30
5 5 5
2 4 3
1 1000000000 1000000000
1 1000000000 999999999
3 2 5
3 2 6
output
0
36
0
0
1999999994
1999999994
2
4

考点

brute force;greedy;math;sortings

AC代码

#include <bits/stdc++.h>
#include <time.h>
#include <iomanip>
#define _CRT_SECURE_NO_WARNINGS
const double pi=acos(-1.0);
const int mmax=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
const int maxn=1005;
typedef long long ll;
using namespace std;
int a[maxn];
//int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};int dir[8][2]={{-1,-1},{0,-1},{1,-1},{-1,0},{1,0},{-1,1},{0,1},{1,1}};
//atoi(string.c_str());//setw()<<setfill('0');//fixed<<setprecision();
int main () {
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int t;cin>>t;
while(t--){
for(int i=1;i<=3;i++){
cin>>a[i];
}
sort(a+1,a+4);
if(a[1]<a[3])
a[1]++;
if(a[3]>a[1])
a[3]--;
cout<<2*(a[3]-a[1])<<endl;
}
return 0;
}
• 点赞 1
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

呵呵的先生

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
12-28 418
09-20 628

09-02 675
06-18 78
02-01 241
10-24 227