codeforces 710B B. Optimal Point on a Line(数学)

题目链接:

B. Optimal Point on a Line

题意:

给出n个点,问找出一个点使得这个点到所有的点的距离和最小;

思路:

所有点排序后的中位数;这是一个结论;

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<‘0‘||CH>‘9‘;F= CH==‘-‘,CH=getchar());
    for(num=0;CH>=‘0‘&&CH<=‘9‘;num=num*10+CH-‘0‘,CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + ‘0‘);
    putchar(‘\n‘);
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=3e5+10;
const int maxn=1e3+20;
const double eps=1e-12;

int x[N];

int main()
{
    int n;
    read(n);
    For(i,1,n)read(x[i]);
    sort(x+1,x+n+1);
    if(n%2==1)cout<<x[n/2+1];
    else cout<<x[n/2];

    return 0;
}

  

时间: 2024-10-26 08:18:40

codeforces 710B B. Optimal Point on a Line(数学)的相关文章

CodeForces 710B Optimal Point on a Line (数学,求中位数)

题意:给定n个坐标,问你所有点离哪个近距离和最短. 析:中位数啊,很明显. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring>

Codeforces Round #247 (Div. 2) B - Shower Line

模拟即可 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ vector<int> a(5); for(int i = 0; i < 5; ++ i) a[i] = i; int g[5][5]; for(int i = 0 ; i < 5; ++ i){ for(int j = 0 ; j < 5; ++

codeforces#253 D - Andrey and Problem里的数学知识

这道题是这样的,给主人公一堆事件的成功概率,他只想恰好成功一件. 于是,问题来了,他要选择哪些事件去做,才能使他的想法实现的概率最大. 我的第一个想法是枚举,枚举的话我想到用dfs,可是觉得太麻烦. 于是想是不是有什么规律,于是推导了一下,推了一个出来,写成代码提交之后发现是错的. 最后就没办法了,剩下的时间不够写dfs,于是就放弃了. 今天看thnkndblv的代码,代码很短,于是就想肯定是有什么数学规律,于是看了一下, 果然如此. 是这样的,还是枚举,当然是有技巧的,看我娓娓道来. 枚举的话

【模拟】Codeforces 710B Optimal Point on a Line

题目链接: http://codeforces.com/problemset/problem/710/B 题目大意: 给N个点的坐标,在X轴上找到最靠左的点使得这个点到N个点距离之和最小. 题目思路: [模拟] 先将N个点坐标排序,找夹在i中间的坐标即为答案.(中间2个数选左边的) 从点坐标是X的点往左移到X+1,代价是X右边的坐标数-左边的坐标数.当X不是给定的坐标时答案是不会变得. 所以最终寻找的这个点一定是N个点中个某个点.并且就在正中中间的位置.如果正中间有两个数,那么这两个数之间的任何

Codeforces Round #153 (Div. 1)APoints on Line

#include<cstdio> #include<cstring> #include<iostream> using namespace std ; const int maxn  = 100010 ; int a[maxn] ; int bit(int l , int r ,int num) { while(l <= r) { int mid = (l + r) >> 1; if(a[mid] <= num) l = mid + 1 ; el

CodeForces 469A. I Wanna Be the Guy(数学)

题目链接:http://codeforces.com/contest/469/problem/A A. I Wanna Be the Guy time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output There is a game called "I Wanna Be the Guy", consisting of n leve

Codeforces Round #273 (Div. 2)C. Table Decorations 数学

C. Table Decorations You have r red, g green and b blue balloons. To decorate a single table for the banquet you need exactly three balloons. Three balloons attached to some table shouldn't have the same color. What maximum number t of tables can be

Codeforces 809A - Do you want a date?(数学+排序)

A. Do you want a date? time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Leha decided to move to a quiet town Vi?kopolis, because he was tired by living in Bankopolis. Upon arrival he immedi

Codeforces 789e The Great Mixing (bitset dp 数学)

Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th type is characterised by its carbon dioxide concentration . Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke