zoukankan      html  css  js  c++  java
  • codeforces710B

    Optimal Point on a Line

     CodeForces - 710B 

    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

    sol:就是找个中位数,分奇偶讨论即可
    #include <bits/stdc++.h>
    using namespace std;
    typedef int ll;
    inline ll read()
    {
        ll s=0;
        bool f=0;
        char ch=' ';
        while(!isdigit(ch))
        {
            f|=(ch=='-'); ch=getchar();
        }
        while(isdigit(ch))
        {
            s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
        }
        return (f)?(-s):(s);
    }
    #define R(x) x=read()
    inline void write(ll x)
    {
        if(x<0)
        {
            putchar('-'); x=-x;
        }
        if(x<10)
        {
            putchar(x+'0'); return;
        }
        write(x/10);
        putchar((x%10)+'0');
        return;
    }
    #define W(x) write(x),putchar(' ')
    #define Wl(x) write(x),putchar('
    ')
    const int N=300005;
    int n,A[N];
    int main()
    {
        int i;
        R(n);
        for(i=1;i<=n;i++) R(A[i]);
        sort(A+1,A+n+1);
        if(n&1) Wl(A[(n+1)>>1]);
        else Wl(A[n>>1]);
        return 0;
    }
    /*
    input
    4
    1 2 3 4
    output
    2
    */
    View Code
     
  • 相关阅读:
    Android在代码中获取应用签名
    Android之密码的显示与隐藏
    如何把本地项目上传到Github
    Android之移动热修复
    Android之apk优化
    Android之内存泄漏
    Java类加载顺序
    mysql系列-⼀条SQL查询语句是如何执⾏的?
    java操作redis(jedis)常用方法示例
    mybatis拦截器实现通用权限字段添加
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10585252.html
Copyright © 2011-2022 走看看