货仓选址

code

#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e5+10;
int  n,res=0;
int a[N];
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n;
	int i=0;	
	for(i=0;i<n;++i){
		cin>>a[i];
	}
	nth_element(a,a+n/2,a+n);
	
	for(i=0;i<n;++i){
		cout<<a[i]<<' ';
	}
	cout<<'\n';
	
	for(i=0;i<n/2;++i){
		//res+=abs(a[i]-a[n>>1]);
		res+=abs(a[n-1-i]-a[i]);
	}
	cout<<res; 
	return 0;
}

quetion

货仓选址
   题目
   提交记录
   讨论
   题解
   视频讲解

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

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

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

输入格式
第一行输入整数 N。

第二行 N 个整数 A1∼AN。

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

数据范围
1≤N≤100000,
0≤Ai≤40000
输入样例:
4
6 2 9 1
输出样例:
12
posted @ 2022-03-16 14:38  ethon-wang  阅读(27)  评论(0编辑  收藏  举报