跳到正文

题解

题解:P13080 [NOISG 2017] Best Places / 最佳选址

Clouds_dream约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年7月5日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 1 分钟
获赞
2
题目
P13080

版本

  1. v1当前

一道水题。

题目大意

题目传送门

题目分析

我们需要找到一个点,使得所有选手到这个点的曼哈顿距离之和最小。为了最小化这个总和,xx 和 yy 应该分别取所有 xix_i 和 yiy_i 的中位数,这是因为中位数在绝对偏差最小化问题中是最优解。

注意

  • 当 nn 为偶数时,选择中间两个数中的任意一个都可以。
  • 由于奇数被整除时会向下取整,所以要加 1。

代码实现

C++
#include <bits/stdc++.h>

using namespace std;

#define endl '\n'

#define int long long

#define fst ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

const int N = 1e6+10, M = 1e8 + 10;

int n;
int xz[N],yz[N];//每个点的坐标

signed main()
{
	fst
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>xz[i]>>yz[i];
	}
	sort(xz+1,xz+n+1);//排序方便找中位数
	sort(yz+1,yz+n+1);
	int mx,my;
	if(n%2==0)mx=xz[n/2],my=yz[n/2];
	else mx=xz[n/2+1],my=yz[n/2+1];
	cout<<mx<<' '<<my;
	return 0;
}
原文