hdu 1173

来源:互联网 发布:发票软件哪里下载 编辑:程序博客网 时间:2024/06/07 04:56

数学题 ,因为只能正方向行走,所以总距离dist=sum{|x-xi|+|y-yi|,i=1..n}
所以x为xi中的中位数,y为yi中的中位数时,总距离最小

代码:

#include <iostream>#include<cstdio>#include<algorithm>using namespace std;const int maxn=1000005;double xp[maxn];   // x positiondouble yp[maxn]; // y positionint main(){   int n;   double x,y;   while(scanf("%d",&n)!=EOF){        if(n==0)break;        for(int i=0;i<n;i++){            scanf("%lf%lf",&xp[i],&yp[i]);        }        sort(xp,xp+n);        sort(yp,yp+n);        if(n%2==0){            x=(xp[n/2]+xp[n/2-1])/2.0;            y=(yp[n/2]+yp[n/2-1])/2.0;        }else{            x=xp[n/2];            y=yp[n/2];        }        printf("%.2lf %.2lf\n",x,y);   }    return 0;}
原创粉丝点击