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

 



转载于:https://www.cnblogs.com/lyf-acm/p/5798173.html

### Codeforces Problem or Contest 998 Information For the specific details on Codeforces problem or contest numbered 998, direct references were not provided within the available citations. However, based on similar structures observed in other contests such as those described where configurations often include constraints like `n` representing numbers of elements with defined ranges[^1], it can be inferred that contest or problem 998 would follow a comparable format. Typically, each Codeforces contest includes several problems labeled from A to F or beyond depending on the round size. Each problem comes with its own set of rules, input/output formats, and constraint descriptions. For instance, some problems specify conditions involving integer inputs for calculations or logical deductions, while others might involve more complex algorithms or data processing tasks[^3]. To find detailed information regarding contest or problem 998 specifically: - Visit the official Codeforces website. - Navigate through past contests until reaching contest 998. - Review individual problem statements under this contest for precise requirements and examples. Additionally, competitive programming platforms usually provide comprehensive documentation alongside community discussions which serve valuable resources when exploring particular challenges or learning algorithmic solutions[^2]. ```cpp // Example C++ code snippet demonstrating how contestants interact with input/output during competitions #include <iostream> using namespace std; int main() { int n; cin >> n; // Process according to problem statement specifics } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值