杭电 1007 不知道错在哪里,为什么一直wrong answer?
#include<stdio.h>#include<algorithm>//!!!!!!记住sort的头函数#include<cmath>#defineMAX100000...
#include <stdio.h>
#include <algorithm> //!!!!!!记住 sort 的头函数
#include <cmath>
#define MAX 100000
using namespace std;
struct Point
{
double x,y;
};
Point p[MAX];
Point px[MAX];
double min(double a,double b)
{
return a<b?a:b;
}
bool cmpx(Point a,Point b)
{
return a.x < b.x;
}
bool cmpy(Point a,Point b)
{
return a.y < b.y;
}
double dis(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
double min_dis(int s,int e)
{
if(s == e)
{
return 100000000;
}
if(s+1 == e)
{
return dis(p[s],p[e]);
}
if(s+2 == e)
{
return min(dis(p[s],p[e]),min(dis(p[s],p[s+1]),dis(p[s+1],p[e])));
}
int i,j,k;
int mid = (s+e)/2;
int minx = min(min_dis(s,mid),min_dis(mid+1,e));
k = 0;
for(i=s;i<=e;i++)
{
if(fabs(p[i].x-p[mid].x)<minx)
{
px[k++] = p[i];
}
}
sort(px,px+k,cmpy);
double temp = 0;
for(i=0;i<k-1;i++)
{
for(j=i+1;j<=i+7;j++)
{
if(j>=k) break;
if(px[j].y-px[i].y>minx) break;
else
{
temp = dis(px[i],px[j]);
minx = min(minx,temp);
}
}
}
return minx;
}
int main()
{
int n,i;
while(scanf("%d",&n)!=EOF&&n)
{
for(i=0;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
sort(p,p+n,cmpx);
printf("%.2lf\n",min_dis(0,n-1)/2);
}
return 0;
} 展开
#include <algorithm> //!!!!!!记住 sort 的头函数
#include <cmath>
#define MAX 100000
using namespace std;
struct Point
{
double x,y;
};
Point p[MAX];
Point px[MAX];
double min(double a,double b)
{
return a<b?a:b;
}
bool cmpx(Point a,Point b)
{
return a.x < b.x;
}
bool cmpy(Point a,Point b)
{
return a.y < b.y;
}
double dis(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
double min_dis(int s,int e)
{
if(s == e)
{
return 100000000;
}
if(s+1 == e)
{
return dis(p[s],p[e]);
}
if(s+2 == e)
{
return min(dis(p[s],p[e]),min(dis(p[s],p[s+1]),dis(p[s+1],p[e])));
}
int i,j,k;
int mid = (s+e)/2;
int minx = min(min_dis(s,mid),min_dis(mid+1,e));
k = 0;
for(i=s;i<=e;i++)
{
if(fabs(p[i].x-p[mid].x)<minx)
{
px[k++] = p[i];
}
}
sort(px,px+k,cmpy);
double temp = 0;
for(i=0;i<k-1;i++)
{
for(j=i+1;j<=i+7;j++)
{
if(j>=k) break;
if(px[j].y-px[i].y>minx) break;
else
{
temp = dis(px[i],px[j]);
minx = min(minx,temp);
}
}
}
return minx;
}
int main()
{
int n,i;
while(scanf("%d",&n)!=EOF&&n)
{
for(i=0;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
sort(p,p+n,cmpx);
printf("%.2lf\n",min_dis(0,n-1)/2);
}
return 0;
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询