A13127 | Three Friends
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
The first line of the input contains one integer $q$ ( $1 \le q \le 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 \le a, b, c \le 10^9$ ) — initial positions of the first, second and third friend correspondingly. The positions of friends can be equal.
The next $q$ lines describe test cases. The $i$ -th test case is given as three integers $a, b$ and $c$ ( $1 \le a, b, c \le 10^9$ ) — initial positions of the first, second and third friend correspondingly. The positions of friends can be equal.
输出格式
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.
输入输出样例
输入 #1
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
输出 #1
0 36 0 0 1999999994 1999999994 2 4
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted