• AIM Tech Round 3 (Div. 2) B


    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.

    Examples
    input
    3 10
    1 7 12
    output
    7
    input
    2 0
    11 -10
    output
    10
    input
    5 0
    0 0 1000 0 0
    output
    0
    Note

    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 is12 - 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.

    题意:给你一个起点和n个坐标,问 至少去n-1个地方的总共距离是多少

    解法:先排序,去n-1个点,那么就是去1到n-1,或者是2到n,讨论一下就可以了,开始点先去1,再转身去n-1,或者先去n-1再转身去1(开始点先去2,再转身去n,或者先去n再转身去2)

    #include<bits/stdc++.h>
    using namespace std;
    int a[100005],pos,n;
    long long sum[100005];
    struct P
    {
        int ans,cot;
    }He[100005];
    bool cmd(P x,P y)
    {
        return x.ans<y.ans;
    }
    int main()
    {
    
        cin>>n>>pos;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        sort(a+1,a+n+1);
        if(n==1)
        {
            cout<<"0"<<endl;
        }
        else if(n==2)
        {
            cout<<min(abs(pos-a[1]),abs(pos-a[2]));
        }
        else
        {
            int pos1=abs(pos-a[1]);
            int pos2=abs(a[1]-a[n-1]);
            int pos3=abs(pos-a[n-1]);
            int pos4=abs(pos-a[2]);
            int pos5=abs(pos-a[n-1]);
            int pos6=abs(a[n]-a[2]);
            int pos7=abs(pos-a[n]);
            cout<<min(min(pos1+pos2,pos3+pos2),min(pos4+pos6,pos7+pos6));
        }
        return 0;
    }
    

      

  • 相关阅读:
    POJ 1082 博弈推规律
    博弈论总结
    POJ 2502 Subway 关键在建图
    【算法设计与数据结构】为何程序员喜欢将INF设置为0x3f3f3f3f?
    一位ACMer过来人的心得
    POJ1724 Dij队列优化邻接表
    Dijkstra队列优化矩阵版
    Uva247 Floyd传递闭包+map处理
    简爬新浪新闻网
    爬href链接
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/5910999.html
Copyright © 2020-2023  润新知