1096 distance and minimum base time limit: 1 seconds space limit: 131072 KB score: 20 Difficulty: 3-level algorithm the collection is concerned with n points on the x-axis, and a point on the x-axis makes it to the minimum distance of the N-point, and the sum of the minimum distance is output. Input
Line 1th: number of points N. (2 <= N <= 10000)
Line 2-n + 1: The position of the point. ( -10^9 <= p[i] <= 10^9)
Output
The sum of the output minimum distance
Input example
5
-1
-3
0
7
9
Output example
20
Take the point with the smallest total distance, where n is odd, the point should be the number of subscripts (N/2) in these n points (the subscript starts at 0), or even when taken (N/2) or (n/2-1).
#include <iostream>
#include <algorithm>
using ll=long long;
using namespace std;
int a[10005];
ll ABS (ll num)
{
if (num<0)
return-num;
return num;
}
int main ()
{
int n,i;
cin>>n;
for (i=0; i<n; ++i)
{
cin>>a[i];
}
Sort (a,a+n);
LL sum=0;
int minnum=a[n/2-1];
for (i=0; i<n; ++i)
{
sum+=abs (minnum-a[i]);
}
cout<<sum<<endl;
return 0;
}