codeforces AIM Tech Round 3 (Div. 2) B. Checkpoints

题目链接:点击打开链接

题目大意:给定x轴上的一些整数点,问从m点出发到n-1个点至少要走多少米?

题目解析:读完题目之后感觉想暴力一发,就是从m走到最右边再走回来走到最左边,这样相当于走了两遍m的右半部分,再算上另一半一共两种答案,去一个最小值就好了。超时了,想了好半天发现自己思维定式了,把给定的所有点都过一遍只留下一个边界就可以,所以问题转换为从m到两个边界的问题,所以排序之后取四种情况的最小值即可。


#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstring>
#include <iomanip>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#define LL long long
#define pr pair<int,int>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

using namespace std;

const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 100005;

LL arr[maxn];

int main(){
    LL n,m;
    scanf("%lld %lld",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%lld",&arr[i]);
    }
    arr[n] = m; n += 1;
    sort(arr,arr+n);

    LL q = abs(m - arr[0]) + abs(m - arr[n-2]) * 2;
    LL w = abs(m - arr[0]) * 2 + abs(m - arr[n-2]);
    LL e = abs(m - arr[1]) + abs(m - arr[n-1]) * 2;
    LL r = abs(m - arr[1]) * 2 + abs(m - arr[n-1]);

    printf("%lld\n",min(q,min(w,min(e,r))));
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值