zoukankan      html  css  js  c++  java
  • [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
    ", A[(n>>1)+1]);
    	else printf("%d
    ", A[n>>1]);
    	
    	return 0;
    }
    
  • 相关阅读:
    C# 验证IP地址、Email格式、URl网址
    如何创建、安装和调试Windows服务
    C#发送Email邮件方法总结
    C#调用java类、jar包方法。
    未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序
    在已经存在的表上创建索引
    Windows下的.NET+ Memcached安装
    把表从Access2007导出到Sql Server
    FusionCharts参数说明
    Sublime Text 3 之配置package control
  • 原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5806721.html
Copyright © 2011-2022 走看看