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

Educational Codeforces Round 16 B

时间:2016-08-24 11:11:04      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:

Description

You are given n points on a line with their coordinates xi. Find the point x so the sum of distances to the given points is minimal.

Input

The first line contains integer n (1 ≤ n ≤ 3·105) — the number of points on the line.

The second line contains n integers xi ( - 109 ≤ xi ≤ 109) — the coordinates of the given n points.

Output

Print the only integer x — the position of the optimal point on the line. If there are several optimal points print the position of the leftmost one. It is guaranteed that the answer is always the integer.

Example
input
4
1 2 3 4
output
2
题意:找出一个点,以外的点距离它之和最小
解法:排序找中间的数
#include<bits/stdc++.h>
using namespace std;
int a[3*100005];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    sort(a+1,a+1+n);
    if(n==1)
    {
        cout<<a[1]<<endl;
    }
    else if(n&1)
    {
    cout<<a[n/2+1]<<endl;
    }
    else
    {
        cout<<a[n/2]<<endl;
    }
    return 0;
}

  

Educational Codeforces Round 16 B

标签:

原文地址:http://www.cnblogs.com/yinghualuowu/p/5802003.html

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