题目描述:
There are always some problems that seem simple but is difficult to solve.
ZYB got N distinct points on a two-dimensional plane. He wants to draw a magic line so that the points will be divided into two parts, and the number of points in each part is the same. There is also a restriction: this line can not pass through any of the points.
Help him draw this magic line.
输入描述:
There are multiple cases. The first line of the input contains a single integer T (1≤T≤10000), indicating the number of cases. For each case, the first line of the input contains a single even integer N (2≤N≤1000), the number of points. The following N N N lines each contains two integers xi,yi (|xi,yi|≤1000), denoting the x-coordinate and the y-coordinate of the i-th point. It is guaranteed that the sum of N over all cases does not exceed 2×105.
输出描述:
For each case, print four integers x1,y1,x2,y2 in a line, representing a line passing through (x1,y1) and (x2,y2). Obviously the output must satisfy (x1,y1) ≠ (x2,y2). The absolute value of each coordinate must not exceed 109. It is guaranteed that at least one solution exists. If there are multiple solutions, print any of them.
解:
题目就是求一个直线,把给定的点分成两部分,每部分的点的个数相等,输出的是直线上整点的坐标。一开始以为是一个计算几何的题,后来才发现根本不用那么麻烦,只需要把点都读进来,按照坐标排一下序,找到最中间的两个点,如果这两个点的横坐标相同,那么就一个点的横坐标往左移一下,一个往右移一下,之后纵坐标直接固定一个值,另一个点的纵坐标就利用对称来求;如果横坐标不同,那直接输出就行了。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <stack>
using namespace std;
const double eps = 1e-8;
const int maxn = 1e3+5;
int n;
struct poi
{
int x, y;
}p[maxn];
bool cmp(poi a, poi b)
{
if(a.x == b.x)
return a.y < b.y;
else
return a.x < b.x;
}
int main ()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
bool f1 = 1, f2 = 1;
for(int i = 1; i <= n; ++i)
scanf("%d%d", &p[i].x, &p[i].y);
sort(p+1, p+n+1, cmp);
if(p[n/2].x == p[n/2+1].x)
printf("%d 1000000 %d %d\n", p[n/2].x-1, p[n/2+1].x+1, p[n/2].y+p[n/2+1].y-1000000);
else
printf("%d 1000000 %d -1000000\n", p[n/2].x, p[n/2+1].x);
}
return 0;
}