求一个集合内最长的两点距离
#include<iostream>
#include<cstdio>
#include<math.h>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
#include<vector>
using namespace std;
int top=0,sta[1000+10],n;
typedef struct Node{
int x,y,index;
};
Node node[1000+10];
double chaji(Node n0,Node n1,Node n2){
return (n1.x-n0.x)*(n2.y-n0.y)-(n2.x-n0.x)*(n1.y-n0.y);
}
double dis(Node n1,Node n2){
return sqrt((n2.x-n1.x)*(n2.x-n1.x)+(n2.y-n1.y)*(n2.y-n1.y));
}
bool cmp(Node n1,Node n2){
double fz=chaji(node[0],n1,n2);
if(fz>0) return 1;
if(fz<0) return 0;
return dis(node[0],n1)<dis(node[0],n2);
}
void tubao(){
sta[0]=0;
sta[1]=1;
top=1;
for(int i=2;i<n;i++){
while(top>0&&chaji(node[sta[top-1]],node[sta[top]],node[i])<=0) top--;
sta[++top]=i;
}
}
double getmaxlen(){
int q=1;
double sum=0;
sta[top]=0;
for(int i=0;i<top;i++){
while(chaji(node[sta[i]],node[sta[i+1]],node[sta[q+1]])>
chaji(node[sta[i]],node[sta[i+1]],node[sta[q]])) q=(q+1)%top;
sum=max(sum,max( dis(node[sta[i]] , node[sta[q]] ),
dis(node[sta[i+1]] , node[sta[q+1]])));
}
return sum;
}
int main(){
cin>>n;
int min_index=0;
for(int i=0;i<n;i++){
scanf("%d%d",&node[i].x,&node[i].y);
node[i].index=i;
if(node[i].y<node[min_index].y||(node[i].y==node[min_index].y&&node[i].x<node[min_index].x))
min_index=i;
}
swap(node[min_index],node[0]);
sort(node+1,node+n,cmp);
tubao();
printf("%.4f",getmaxlen());
return 0;
}
还要继续看。。不是很懂