題目連接:https://ac.nowcoder.com/acm/contest/883/Hhtml
題意:給定n個點(n爲偶數),求一條直線使得n個點平均分散在直線兩端,即每端n/2個點。node
思路:把n個點按x升序排列,x相等時按y升序排列,這時候咱們取第n/2個點和第n/2+1個點,以它兩爲界限,把n個點均分。由於n個點的座標<=1e3,而咱們的直線的點能夠<=1e9,那麼必定能夠找到一條很陡的直線知足條件。spa
圖片來自:https://www.cnblogs.com/st1vdy/p/11245932.htmlcode
AC代碼:htm
#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; }