More than 2019 cattle off summer school camp (third) - H Magic Line (computational geometry)

Topic link: https: //ac.nowcoder.com/acm/contest/883/H

The meaning of problems: Given n points (n is an even number), such that a line seeking n points evenly distributed across the straight line, i.e., each end of the n / 2 points.

Ideas: the n points by x in ascending order, according to y ascending equal x, which we take the first n / 2 points and the second n / 2 + 1 points, in which two of the boundaries, the n points are Minute. Because the coordinate points n <= 1e3, and our point of straight lines can be <= 1e9, then you can find a very steep straight line to meet the conditions.

Photo: https: //www.cnblogs.com/st1vdy/p/11245932.html

AC Code:

#include<cstdio>
#include<algorithm>
using namespace std;

int T,n;

struct node{
    int x,y;
}pt[1005];

bool cmp(node a,node b){
    if(a.x==b.x)
        return a.y<b.y;
    return a.x<b.x;
}

int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
            scanf("%d%d",&pt[i].x,&pt[i].y);
        sort(pt+1,pt+n+1,cmp);
        int x1=pt[n/2].x,y1=pt[n/2].y;
        int x2=pt[n/2+1].x,y2=pt[n/2+1].y;
        int xx1,xx2,yy1,yy2;
        if((y1+y2)>=0)
            yy1=1e9,yy2=y1-(yy1-y2);
        else
            yy2=-1*1e9,yy1=y2+(y1-yy2);
        if((x1+x2)%2==0)
            xx1=(x1+x2)/2-1,xx2=xx1+2;
        else
            xx1=x1+(x2-x1)/2,xx2=xx1+1;
        printf("%d %d %d %d\n",xx1,yy1,xx2,yy2);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/FrankChen831X/p/11258391.html