Description
Analysis
首先,观察题目。
f(y)=∑i=1n|y−xi|
稍微学过小学的都会想起 中位数
于是,不约而同地,就很想把所有的 li,ri 放在一起排序,却难以证出来
事实上,我们可以如此考虑
很显然,把红线往左的(边界)个数记作a,右边记作b
显然的,为使答案最优,有
|a−b|<=1
证毕!
Code
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define oo 2147483647
using namespace std;
const int N = 100100;
int n,a[N * 2],tot;
int main()
{
//freopen("D:/LiuYuanHao/4882_1.in","r",stdin);
freopen("linear.in","r",stdin);freopen("linear.out","w",stdout);
scanf("%d", &n);
for (int i = 1;i <= n;i ++) scanf("%d%d", &a[++tot], &a[++tot]);
sort(a + 1,a + 1 + tot);
printf("%d %d", a[tot/2], a[tot/2+1]);
}