zoukankan      html  css  js  c++  java
  • 货仓选址

    货仓选址

    题目描述

    在一条数轴上有 N 家商店,它们的坐标分别为 A1~AN

    现在需要在数轴上建立一家货仓,每天清晨,从货仓到每家商店都要运送一车商品。

    为了提高效率,求把货仓建在何处,可以使得货仓到每家商店的距离之和最小。

    输入格式

    第一行输入整数N。

    第二行N个整数A1~AN。

    输出格式

    输出一个整数,表示距离之和的最小值。

    数据范围

    1≤N≤100000,0≤Ai≤40000

    输入样例

    3
    4
    6 2 9 1
    

    输出样例

    12
    

    题目思路

    利用均值不等式,最中间即为最短

    #include<cstdio>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    
    const int N = 1e5+10;
    int a[N];
    
    int main()
    {
        int n,r = 0;
        cin >> n;
        for(int i=0;i<n;i++)cin >> a[i];
        sort(a,a+n);
        for(int i=0;i<n;i++)r += abs(a[i]-a[n/2]);
        cout << r;
        return 0;
    }
    
  • 相关阅读:
    结构化建模分析
    qemusystemriscv64 machine \?
    git clone commit
    riscv gdb machine mode
    error: src refspec main does not match any.
    riscv ecall
    git windows
    fixedlink
    iperf交叉编译
    每日学习
  • 原文地址:https://www.cnblogs.com/fsh001/p/14274969.html
Copyright © 2011-2022 走看看