| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
这题用凸包很容易过(用时207ms),贴我的代码作参考:/*此题用凸包算法可以过,
对于点对多的。只需要比较凸包边上的点。因为中间的点不可能是点对间距离最大的点*/
#include"stdio.h"
#include"math.h"
#include"stdlib.h"
#define N 50001
struct node
{
int x;
int y;
}stack[N],point[N];
int top=-1;
int cmp(const void *a,const void *b)
{
struct node *aa=(struct node*)a;
struct node *bb=(struct node*)b;
return -(aa->x-point[0].x)*(bb->y-point[0].y)+(aa->y-point[0].y)*(bb->x-point[0].x)>=0?1:-1;/*按极坐标逆时针排序*/
}
void push(struct node a)/*进栈*/
{
if(top!=N-1)
{
top++;
stack[top].x=a.x;
stack[top].y=a.y;
}
else
printf("too flow!\n");
}
void pop()/*出栈*/
{
if(top!=-1)
top--;
}
int main()
{
int n;
int i,j,k;
struct node temp1;
float temp;
scanf("%d",&n);
scanf("%d%d",&point[0].x,&point[0].y);
for(i=1;i<n;i++)
{
scanf("%d%d",&point[i].x,&point[i].y);
if(point[i].y<point[0].y||(point[i].y==point[0].y&&point[i].x<point[0].x))/*目的只有一个:使p[0]是最低点,使之作为极坐标的原点*/
{
temp1=point[i];
point[i]=point[0];
point[0]=temp1;
}
}
qsort(point,n,sizeof(struct node),cmp);/*极坐标排序*/
/*验证排序*/
/*printf("\n");
for(i=0;i<n;i++)
printf("%d %d\n",point[i].x,point[i].y);
printf("\n");*/
push(point[0]);/*将前两点压入栈*/
push(point[1]);
i=2;
do
{
/*当p[i]和stack[top-1]够成的直线在直线(stack[top]与stack[top-1]成的直线)右边时。不符要求,出栈,并循环检验*/
while((point[i].x-stack[top-1].x)*(stack[top].y-stack[top-1].y)-(point[i].y-stack[top-1].y)*(stack[top].x-stack[top-1].x)>0)/*注意不要加等号,因为在凸包边上的点也要考虑*/
pop();
push(point[i]);/*每个点至少进栈一次*/
i++;
}while(i<n);
/*以上步骤剩下的栈内的点就是凸包的边点*/
k=-1;/*设置最低值*/
for(i=0;i<top;i++)
for(j=i+1;j<=top;j++)/*对凸包边点进行距离比较,大大缩小了运算时间*/
if(pow(stack[i].x-stack[j].x,2)+pow(stack[i].y-stack[j].y,2)>=k)
k=pow(stack[i].x-stack[j].x,2)+pow(stack[i].y-stack[j].y,2);
printf("%d\n",k);
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator