曼哈顿距离问题
B. Eastern Exhibition
明显这里我们可以将x与y拆开来考虑
即问题变成了:给定1~n个数a1~n,我们要找出一个数num
使得 |num-a1|+|num-a2|+....+|num-an|最小
答案num就是a1~an中的中位数
如果a1~an中有中位数ai和aj;
那么num可以取 ai,ai+1,.....,aj-1,aj中任何一个数
问题到此时也得以解决:变成了x和y 中中位数xi,xj;yi,yj
说明在x轴上有xj-xi+1个数可以选
在y轴上有yj-yi+1个数可以选
ans=(xj-xi+1)*(yj-yi+1);
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
int t;
int get(vector<int> xs)
{
int xlen = xs.size() - 1;
int posl = xlen / 2, posr = (xlen + 1) / 2;
int xnum = xs[posr] - xs[posl] + 1;
return xnum;
}
int main()
{
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<int> xs, ys;
for (int i = 1; i <= n; i++)
{
int x, y;
scanf("%d%d", &x, &y);
xs.push_back(x);
ys.push_back(y);
}
sort(xs.begin(), xs.end());
sort(ys.begin(), ys.end());
cout << (ll)get(xs) * get(ys) << endl;
}
return 0;
}