牛客多校__Magic Line

鏈接: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;
}



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章