码迷,mamicode.com
首页 > 其他好文 > 详细

51nod 1096 距离之和最小【中位数】

时间:2017-11-05 13:06:47      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:input   png   输出   panel   html   clu   color   opened   ble   

基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题
技术分享 收藏
技术分享 关注
X轴上有N个点,求X轴上一点使它到这N个点的距离之和最小,输出这个最小的距离之和。
 
Input
第1行:点的数量N。(2 <= N <= 10000)
第2 - N + 1行:点的位置。(-10^9 <= P[i] <= 10^9)
Output
输出最小距离之和
Input示例
5
-1
-3
0
7
9
Output示例
20
【分析】:注意LL,距离abs
【代码】:
技术分享
#include <bits/stdc++.h>
using namespace std;
#define LL long long
int main()
{
    LL n,ans=0;
    LL a[10000+10];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    for(int i=0;i<n;i++)
    {
        ans+=abs(a[i]-a[n/2]);
    }
    cout<<ans<<endl;
    return 0;
}
View Code

 

51nod 1096 距离之和最小【中位数】

标签:input   png   输出   panel   html   clu   color   opened   ble   

原文地址:http://www.cnblogs.com/Roni-i/p/7787051.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!