zoukankan      html  css  js  c++  java
  • CodeForce 710B Optimal Point on a Line

    Optimal Point on a Line

      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

    题意:
      
    给出N个点所在数轴的位置,找一个点,使其他点到这个点的距离和最小

    思路:
      找中位数,就可以了

    AC代码:
     1 # include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long int ll;
     4 const int MAX = 3 * 1e5 + 1;
     5 ll a[MAX];
     6 int main()
     7 {
     8     ll n;
     9     scanf("%I64d", &n);
    10     ll sum = 0;
    11     for(int i = 1; i <= n; i++)
    12     {
    13         scanf("%I64d", &a[i]);
    14         sum += a[i];
    15     }
    16     
    17     sort(a + 1, a + n + 1);
    18     if(n % 2)
    19         cout << a[n / 2 + 1] << endl;
    20     else
    21         cout << a[n / 2] << endl;
    22     return 0;
    23 }
    View Code


    生命不息,奋斗不止,这才叫青春,青春就是拥有热情相信未来。
  • 相关阅读:
    无休止的项目,何来快感!!
    [From HTTP to AWS][4]使用LibcURL with OpenSSL support
    [From HTTP to AWS][2]Analyze TCP/IP Packets
    The setup of Piaoger
    从Adobe Subscription editions扯到破坏性创新
    SaaS窘境[欣赏然后翻译之]
    Algodoo,很棒的物理引擎
    浮水法POJ2528
    蛤的旅行
    题解 CF712A 【Memory and Crow】
  • 原文地址:https://www.cnblogs.com/lyf-acm/p/5798173.html
Copyright © 2011-2022 走看看