zoukankan      html  css  js  c++  java
  • 2020-2021 ACM-ICPC, Asia Seoul Regional Contest G. Mobile Robot

    原题传送门

    题面

    Mobile robots are nowadays commonly used in various industrial and research sites. You are in charge of controlling (n) mobile robots that explore a very long, narrow and straight cave, which can be seen just as a line. The mobile robots collect data from the environment nearby and have effective mobility with their caterpillar tracks. You can control the (n) mobile robots on your control desk by a wireless control system. The (n) mobile robots you are controlling are labelled with numbers (1) to (n), and are identified by robot (1), robot (2), ..., robot (n−1), and robot (n).

    The mobile robots can also share their collected data to each other by a simple infrared communication protocol, while this robot-to-robot communication only works when the following very strict arrangement is completed for all the (n) mobile robots: the distance between robot (i) and robot (i+1) should be exactly (d) for all (i=1,2,…,n−1), where (d) is a prescribed positive real number, and no two robots should be at the same location in the cave. The location of each mobile robot in the cave is represented by a real number (x) since the cave is very long, very narrow, and very straight, so can be considered a line which stretches limitlessly in both directions. The distance between two mobile robots is thus calculated by the difference of their locations.

    From the current locations of the mobile robots, they now need to share data to each other, and you are going to move them for the robot-to-robot communication. Since the robots are slow and simultaneously move at the same speed along the cave, you want to minimize the maximum distance each robot should travel to waste as little time as possible. During traveling, any two robots are assumed to safely pass by each other at the moment when both are at a common location in the cave. Note hence that currently two or more robots may be at a common location in the cave.

    Given the current locations of the (n) mobile robots, write a program that computes their new locations for the robot-to-robot communication that minimizes the maximum distance each of the (n) robots travels and outputs the minimized maximum distance the robots should travel.

    Input

    Your program is to read from standard input. The input consists of exactly two lines. The first line consists of two integers, (n) and (d (2≤n≤1,000,000,1≤d≤1010)), where (n) denotes the number of mobile robots you are controlling and (d) is the distance that the robots should keep for the robot-to-robot communication. Each mobile robot is identified by a label from (1) to (n). The second line consists of (n) integers, each of which ranges from (−1016) and (1016), representing the current locations of robot (1), robot (2), ..., and robot (n) in this order.

    Output

    Your program is to write to standard output. Print exactly one line consisting of a real number, rounded to the first decimal place, that represents the minimum possible value of the maximum distance the mobile robots should travel for the robot-to-robot communication from the given current locations.

    Examples

    Input

    5 1
    1 3 5 7 9
    

    Output

    2.0
    

    Input

    5 1
    -10 -1 0 1 2
    

    Output

    4.0
    

    Input

    5 1
    1 3 5 9 7
    

    Output

    2.5
    

    Input

    5 1
    1 1 1 1 1
    

    Output

    2.0
    

    题目分析

    题意:有(n)个机器人可供你操控,每个机器人都在一个数轴上,移动时以相同的速度移动。让你求出当每个机器人的间距恰好为(d)时,让机器人移动的最大距离的最小值是多少

    重点

    我们可以先根据第一个机器人来构造一个间距为(d)的等差数列(a),然后再和当前的坐标作差,就能得到机器人移动的距离,去最大值除二保留一位小数即可,即maxa / 2。以样例1为例,这个过程用一个表格表示如下:

    机器人编号 1 2 3 4 5
    初始位置 1 3 5 7 9
    构造位置 1 2 3 4 5
    位移(构造->初始) 0 1 2 3 4

    所以答案就是(2.0)

    但是上面的思路能被下面两组数据hack掉:

    3 1
    1 1 4
    
    5 1
    1 1 4 9 10
    

    以hack的第一组为例,过程用表格表示后为:

    机器人编号 1 2 3
    初始位置 1 1 4
    构造位置 1 2 3
    位移(构造->初始) 0 -1 1

    如果按照上面的思路,那么得到的结果(0.5),但是机器人2号和3号的位移方向不一致,也就是速度方向不一致,违背了题干中速度相同的要求(别忘了速度是矢量)。不过我们可以从机器人3号重新构造,可以再得到下面的这张表:

    机器人编号 1 2 3
    初始位置 1 1 4
    构造位置(符合) 2 3 4
    位移(构造(符合)->初始) 1 2 0

    所以正确的答案就是(1.0)

    再看第二组hack数据:

    机器人编号 1 2 3 4 5
    初始位置 1 1 4 9 10
    构造位置(机器人1) 1 2 3 4 5
    位移(构造->初始) 0 -1 1 5 5
    机器人编号 1 2 3 4 5
    初始位置 1 1 4 9 10
    构造位置(符合) 6 7 8 9 10
    位移(构造(符合)->初始) -5 -6 -4 0 0

    所以答案为(0)

    经过观察可以发现,符合题目要求的最大差值实际上是第一次构造的最大值减去第一次构造的最小值

    那么答案应该为ans = (maxa - mina) / 2

    其他要注意的点:

    1. 由于机器人的间隔是(1),所以构造的位置可以是 (a_i+(i-d)*i),也可以是(a_i+(i+d)*i)
    2. 数据范围较大,会爆double,所以请选择long double,对应的转换符为%Lf
    3. 记得保留一位小数
    4. 请善于使用STL里的函数

    AC代码

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N = 1e6 + 3;
    ll n, d, ans;
    ll loc[N];
    
    inline ll get(ll d){
        vector<ll> a;
        for(int i = 1; i <= n; i++) a.push_back(loc[i] - (loc[1] + (n - i) * d));
        return *max_element(a.begin(), a.end()) - *min_element(a.begin(), a.end());
    }
    
    int main() {
        ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
        cin >> n >> d;
        for (int i = 1; i <= n; i++) cin >> loc[i];
        
        printf("%.1Lf", (long double)min(get(d), get(-d)) / 2);
        
        return 0;
    }
    
  • 相关阅读:
    服务器日常维护
    每日哲言
    JAVA经典算法40题(原题+分析)之分析
    如何在苹果笔记本上装win7系统
    酷派改变者S1(C105/C105-6/C105-8) 解锁BootLoader 并刷入recovery root
    努比亚 Z17 mini s (Nubia NX589J) 解锁BootLoader 并刷入recovery ROOT
    努比亚 Z17s (Nubia NX595J) 解锁BootLoader 并刷入recovery ROOT
    努比亚 N2(Nubia NX575J) 解锁BootLoader 并进入临时recovery ROOT
    三星A3、A5、A7、G7、J5、J7、S6系列等新机型的部分手机解锁 ROOT刷机
    [2月1号] 努比亚全机型ROM贴 最全最新NubiaUI5.0 ROOT 极速体验
  • 原文地址:https://www.cnblogs.com/FrankOu/p/14417674.html
Copyright © 2011-2022 走看看