Codeforces Problem 709B Checkpoints(分类讨论)

此文章可以使用目录功能哟↑(点击上方[+])

比赛链接→AIM Tech Round 3 (Div. 2)

 Codeforces Problem 709B Checkpoints

Accept: 0    Submit: 0
Time Limit: 1 second    Memory Limit : 256 megabytes

 Problem Description

Vasya takes part in the orienteering competition. There are n checkpoints located along the line at coordinates x1, x2, ..., xn. Vasya starts at the point with coordinate a. His goal is to visit at least n - 1 checkpoint in order to finish the competition. Participant are allowed to visit checkpoints in arbitrary order.

Vasya wants to pick such checkpoints and the order of visiting them that the total distance travelled is minimized. He asks you to calculate this minimum possible value.

 Input

The first line of the input contains two integers n and a (1 ≤ n ≤ 100 000,  - 1 000 000 ≤ a ≤ 1 000 000) — the number of checkpoints and Vasya's starting position respectively.

The second line contains n integers x1, x2, ..., xn ( - 1 000 000 ≤ xi ≤ 1 000 000) — coordinates of the checkpoints.

 Output

Print one integer — the minimum distance Vasya has to travel in order to visit at least n - 1 checkpoint.

 Sample Input

3 10
1 7 12
2 0
11 -10
5 0
0 0 1000 0 0

 Sample Output

7
10
0

 Hint

In the first sample Vasya has to visit at least two checkpoints. The optimal way to achieve this is the walk to the third checkpoints (distance is 12 - 10 = 2) and then proceed to the second one (distance is 12 - 7 = 5). The total distance is equal to 2 + 5 = 7.

In the second sample it's enough to visit only one checkpoint so Vasya should just walk to the point  - 10.

 Problem Idea

解题思路:

【题意】
一维坐标系上的定向越野赛,起点为a

当前坐标系上有n个点,须至少到达n-1个点才算完成比赛

问完成比赛的最小路程


【类型】
implementation,sortings

【分析】

虽然题目说至少到达n-1个点才算完成比赛

但是,为了总路程最小,取n-1个就可以了,谁会去取n个?

所以,'至少'可以忽略,也就是我们需从n个点中选n-1个点,使得从a出发,总路程最小

当n=1时,这种情况可以独立出来判断,因为这种情况,比赛直接就完成了,故结果为0

而当n>1时,为了使路程最小,无非就4种情况:


该情况下,不走的点是x1


该情况下,不走的点是xn


该情况下,不走的点是xn


该情况下,不走的点是x1

为什么只有以上4种情况呢?

首先一点是我们不能多次来回走,因为来回走的次数一多,重复走的地段就多了,显然这部分是不需要的

所以我们至多来回走一次

这样就导致不选的点必定处于边缘,也就是x1或xn

所以我们只要取上述4种情况中总路程最小的那个就可以了

剩下的就是一点点小细节,注意一点就行

【时间复杂度&&优化】
O(nlogn)

题目链接→Codeforces Problem 709B Checkpoints

 Source Code

/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 100005;
const int M = 100005;
const int inf = 1000000007;
const int mod = 1000000007;
int x[N];
int main()
{
    int n,a,i;
    scanf("%d%d",&n,&a);
    if(n==1)
    {
        puts("0");
        return 0;
    }
    for(i=0;i<n;i++)
        scanf("%d",&x[i]);
    sort(x,x+n);
    printf("%d\n",min(min(max(x[n-2]-a,0)+max(x[n-2],a)-x[0],max(x[n-1]-a,0)+max(x[n-1],a)-x[1]),min(max(a-x[1],0)+x[n-1]-min(x[1],a),max(a-x[0],0)+x[n-2]-min(x[0],a))));
    return 0;
}

菜鸟成长记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值