1096 Nod and the smallest distance

Source: Internet
Author: User
Tags abs time limit
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.