链接:https://ac.nowcoder.com/acm/contest/883/H
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld
题目描述
There are always some problems that seem simple but is difficult to solve.
ZYB got N\ N 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)T \ (1 \leq T \leq 10000)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)N \ (2 \leq N \leq 1000)N (2≤N≤1000), the number of points. The following $N$ lines each contains two integers xi,yi (∣xi,yi∣≤1000)x_i, y_i \ (|x_i, y_i| \leq 1000)xi,yi (∣xi,yi∣≤1000), denoting the x-coordinate and the y-coordinate of the i\ i i-th point. It is guaranteed that the sum of N\ N N over all cases does not exceed 2×1052 \times 10^52×105.
输出描述:
For each case, print four integers x1,y1,x2,y2x_1, y_1, x_2, y_2x1,y1,x2,y2 in a line, representing a line passing through (x1,y1)(x_1, y_1)(x1,y1) and (x2,y2)(x_2, y_2)(x2,y2). Obviously the output must satisfy (x1,y1)≠(x2,y2)(x_1,y_1) \ne (x_2,y_2)(x1,y1)=(x2,y2). The absolute value of each coordinate must not exceed 10910^9109. It is guaranteed that at least one solution exists. If there are multiple solutions, print any of them.
示例1
输入
复制
1 4 0 1 -1 0 1 0 0 -1
输出
复制
-1 999000000 1 -999000001
题意:找出一条线平均分隔二维平面上的点,使得左右两边的点的数目相等,并输出这条线经过的两个整数点
题解:先取最左最下的点,然后横坐标和纵坐标减减,然后固定横坐标,递减纵坐标,把二维平面的点以该点为中心极角排序,去中间两个点,看是否极角等于0,如果等于0,则三点共线,不符合题意,继续减减,重复这个操作直到找到合法的
#include <bits/stdc++.h>
using namespace std;
double X,Y;
struct point//存储点
{
double x,y;
int xx,yy;
};
double cross(double x1,double y1,double x2,double y2)//计算叉积
{
return (x1*y2-x2*y1);
}
double compare(point a,point b,point c)//计算极角
{
return cross((b.x-a.x),(b.y-a.y),(c.x-a.x),(c.y-a.y));
}
bool cmp2(point a,point b)
{
point c;//原点
c.x = X;
c.y = Y;
if(compare(c,a,b)==0)//计算叉积,函数在上面有介绍,如果叉积相等,按照X从小到大排序
return a.x<b.x;
else return compare(c,a,b)>0;
}
point a[3000];
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
int x1,y1;
int f=1;
for(int i=0;i<n;i++){
scanf("%d%d",&a[i].xx,&a[i].yy);
a[i].x=a[i].xx*1.0;
a[i].y=a[i].yy*1.0;
if(f==1){
x1=a[i].xx,y1=a[i].yy;
f=0;
}
else{
x1=min(x1,a[i].xx);
y1=min(y1,a[i].yy);
}
}
x1--; y1--;
X=x1*1.0;
for(double i=y1*1.0;;i-=1.0){
Y=i;
sort(a,a+n,cmp2);
if(compare(a[n/2], a[n/2-1], point{X, Y})!=0){
break;
}
y1--;
}
int nx1=a[n/2].xx,ny1=a[n/2].yy;
int nx2=a[n/2-1].xx,ny2=a[n/2-1].yy;
printf("%d %d %d %d\n",x1,y1,(nx1+nx2)-x1,(ny1+ny2)-y1);
}
return 0;
}