zoukankan      html  css  js  c++  java
  • codeforces 710B 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
    分析:水题,可是错了4 遍。。。菜啊。题目告诉我们给定一个数n,接下来给你n个在一条直线上的点的坐标,让你在这些点中找到一个点使得它到其他点的距离和最小。说白了就是找呢个排序后的中间点。。我居然去一个个枚举,不T没天理。。
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 const ll INF = 3 *1e10 + 9;
     5 const int maxn = 3 * 1e5+7;
     6 ll a[maxn];
     7 ll abbs(ll x)
     8 {
     9     return x >= 0? x: -x;
    10 }
    11 
    12 int main()
    13 {
    14     ios::sync_with_stdio(false);
    15     cin.tie(0);
    16     int n;
    17     cin >> n;
    18     for(int i =1; i<= n; i++)
    19     {
    20         cin >> a[i];
    21     }
    22     sort(a+1,a+n+1);
    23     if(n&1) cout << a[(n+1)/2] << endl;
    24     else cout << a[n/2] << endl;
    25     return 0;
    26 }
  • 相关阅读:
    Android ContentProvider和getContentResolver
    onContextItemSelected 用法
    Android 控件 之 Menu 菜单
    Android
    Android Cursor类的概念和用法
    android SQLiteOpenHelper使用示例
    JAVA HashMap详细介绍和示例
    HashMap深度解析(二)
    HashMap深度解析(一)
    使用svn遇到的问题---(在编辑器没有配置svn的前提下)
  • 原文地址:https://www.cnblogs.com/PrayG/p/5809997.html
Copyright © 2011-2022 走看看