2020-2021 ACM-ICPC, Asia Seoul Regional Contest G. Mobile Robot(思维)

传送门

题意:

给出 n n n 个机器人的位置,要求移动这些机器人,使其满足第 i i i 个机器人和第 i + 1 i+1 i+1 个机器人的距离刚好为 d d d

求这些机器人中的最大移动距离的最小值。

题解:

先假设第一个机器人不动,那么可以求出其他机器人的移动距离。

m a ma ma 为向左移动的最大距离, m i mi mi 为向右移动的最大距离, m i mi mi为负数,那么当移动第一个机器人时, m a ma ma m i mi mi 会发变化,如果第一个机器人向右移动, m a ma ma会减小,但 m i mi mi的绝对值会变大,反之向左移动也一样。

所以最优解就是两个数的绝对值中间的数,即 ( m a + a b s ( m i ) ) / 2 = ( m a − m i ) / 2 (ma+abs(mi))/2=(ma-mi)/2 (ma+abs(mi))/2=(mami)/2

代码:

#pragma GCC diagnostic error "-std=c++11"
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<ctime>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
const int mod=1e9+7;
const int MAXN=1e6+5;
const int inf=0x3f3f3f3f;
ll a[MAXN];
vector<ll> ans;
int main()
{
    int n;
    ll d;
    scanf("%d%lld", &n, &d);
    for (int i = 1; i <= n;i++){
        scanf("%lld", &a[i]);
    }
    ll now = a[1];
    for (int i = 1; i <= n;i++){
        ans.push_back(a[i] - now);
        now += d;
    }
    ll s1 = *max_element(ans.begin(), ans.end())-*min_element(ans.begin(),ans.end());
    ans.clear();
    now = a[1];
    for (int i = 1; i <= n; i++) {
        ans.push_back(a[i] - now);
        now -= d;
    }
    ll s2 = *max_element(ans.begin(), ans.end()) - *min_element(ans.begin(), ans.end());
    s1 = min(s1, s2);
    if(s1%2==0){
        printf("%lld.0", s1 / 2);
    } 
    else{
        printf("%lld.5", s1 / 2);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值