Codeforces--1272A--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.
输入描述:
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.
输出描述:
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.
输入:
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
输出:
0
36
0
0
1999999994
1999999994
2
4
题意:
三个人都可以向左或向右移动一次,当然了他们也可以选择不动,问他们移动后两两之间距离的最小距离和。
题解
直接暴力
代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long ll;
ll a[5],b[5],c[5];

int cal(ll x){
    if(x < (ll)0) x = (ll)-1 * x;
    return x;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%lld%lld%lld",&a[0],&b[0],&c[0]);
        a[1] = a[0] - 1;
        a[2] = a[0] + 1;
        b[1] = b[0] - 1;
        b[2] = b[0] + 1;
        c[1] = c[0] - 1;
        c[2] = c[0] + 1;
        ll minn = 4000000000;
        for(int i = 0; i < 3; i ++){
            for(int j = 0; j < 3; j ++){
                for(int k = 0; k < 3; k ++){
                    ll dis = 0;
                    dis += cal(a[i] - b[j]);
                    dis += cal(a[i] - c[k]);
                    dis += cal(b[j] - c[k]);
                    minn = min(minn,dis);
                }
            }
        }
        printf("%lld\n",minn);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值