博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【CodeForces 1272A --- Three Friends】
阅读量:2038 次
发布时间:2019-04-28

本文共 2184 字,大约阅读时间需要 7 分钟。

【CodeForces 1272A --- Three Friends】

题目来源:

Description

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.

Sample 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

Sample Output

0

36
0
0
1999999994
1999999994
2
4

AC代码:

#include 
using namespace std;#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)#define endl '\n'using ll = long long;int main(){
SIS; ll T,a,b,c; cin >> T; while(T--) {
cin >> a >> b >> c; cout << max((ll)0,abs(a-b)+abs(a-c)+abs(b-c)-4) << endl; } return 0;}

转载地址:http://asyof.baihongyu.com/

你可能感兴趣的文章
Android性能优化——实例
查看>>
Android性能调优工具TraceView介绍
查看>>
Android(Java)利用findbugs进行代码静态检查
查看>>
Android ImageCache图片缓存,使用简单,支持预取,支持多种缓存算法,支持不同网络类型,扩展性强
查看>>
Eclipse安装SVN插件
查看>>
windows上如何github使用
查看>>
github for Windows使用介绍
查看>>
Eclipse启动时fail to create Java Virtual Machine问题的解决
查看>>
iOS 删除文件夹下所有文件
查看>>
HTTP Live Streaming直播(iOS直播)技术分析与实现
查看>>
mp4文件格式解析
查看>>
HLS协议实现
查看>>
iOS下ffmepg开发的一些参考资料汇总
查看>>
使用iOS开源库SKPSMTPMessage实现邮件发送
查看>>
RTMP,RTSP,HLS比较与分析
查看>>
网络电视台(基于apple m3u8的ts文件)设计与实现(一)
查看>>
说说 IOS mms流媒体(网络电台)播放
查看>>
流媒体传输协议
查看>>
抛开flash,自己开发实现C++ RTMP直播流播放器
查看>>
RTSP协议转换RTMP直播协议
查看>>