当前位置: 代码迷 >> 综合 >> Codeforces Round #703 (Div. 2) B. Eastern Exhibition(思维)
  详细解决方案

Codeforces Round #703 (Div. 2) B. Eastern Exhibition(思维)

热度:93   发布时间:2023-12-21 00:01:59.0

题目链接:https://codeforces.com/contest/1486/problem/B

You and your friends live in n houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points (x1,y1) and (x2,y2) is |x1?x2|+|y1?y2|, where |x| is the absolute value of x.

Input
First line contains a single integer t (1≤t≤1000) — the number of test cases.

The first line of each test case contains a single integer n (1≤n≤1000). Next n lines describe the positions of the houses (xi,yi) (0≤xi,yi≤10^9).

It’s guaranteed that the sum of all n does not exceed 1000.

Output
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.


分析

我们发现这个问题 x 轴和 y 轴可以独立开来考虑,那么问题就变成了一维数轴上的问题:某点到数轴上一些点距离最短的点有几个,如果是奇数个点,那就是最中间的那个,如果是偶数,就是中间两个点以及中间所有点。x 轴和 y 轴的结果相乘就是答案。
所以如果是奇数个点答案肯定是 1 。

代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;const int maxn = 1e3 + 5;
vector <ll > X, Y;
int main() {
    int t; cin >> t;while (t--) {
    int n; scanf("%d", &n);X.clear(), Y.clear();for (int i = 1, l, r; i <= n; ++i) {
    scanf("%d%d", &l, &r);X.push_back(l);Y.push_back(r);}sort(X.begin(), X.end());sort(Y.begin(), Y.end());if (n & 1) {
    cout << 1 << endl;continue;} else {
    cout << (X[n / 2] - X[n / 2 - 1] + 1)* (Y[n / 2] - Y[n / 2 - 1] + 1) << endl;}}return 0;
} 
  相关解决方案