[Educational Codeforces Round 16]B. Optimal Point on a Line
[Educational Codeforces Round 16]B. 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.
输入
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.
输出
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.
输入示例
4 1 2 3 4
输出示例
2
数据规模及约定
见“输入”
题解
排序输出中位数。
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <stack> #include <vector> #include <queue> #include <cstring> #include <string> #include <map> #include <set> using namespace std; const int BufferSize = 1 << 16; char buffer[BufferSize], *Head, *Tail; inline char Getchar() { if(Head == Tail) { int l = fread(buffer, 1, BufferSize, stdin); Tail = (Head = buffer) + l; } return *Head++; } int read() { int x = 0, f = 1; char c = getchar(); while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); } while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); } return x * f; } #define maxn 300010 int n, A[maxn]; int main() { n = read(); for(int i = 1; i <= n; i++) A[i] = read(); sort(A + 1, A + n + 1); if(n & 1) printf("%d\n", A[(n>>1)+1]); else printf("%d\n", A[n>>1]); return 0; }