主题:第30次编程比赛第一题
yunzhou008 [专家分:410] 发布于 2006-06-09 17:27:00
Problem
给定平面上的n个点,找出它们之间最远的点对。
Input
多组数据,每组第一行n代表点数,接着n行为点的坐标,坐标为整数,不超过Longint范围。n<=30000。
Output
每组一行,最远点对的距离,保留2位小数
Sample Input
4
0 0
1 1
0 1
1 0
Sample Output
1.41
您的代码将在:http://acm.tongji.edu.cn/showproblem.php?problem_id=1082 测试!
回复列表 (共13个回复)
沙发
fenix124 [专家分:70] 发布于 2006-06-10 00:12:00
暂时只想到个O(n^2)的算法,很明显超时,写都懒得写了,期待高人出现。
板凳
iAkiak [专家分:8460] 发布于 2006-06-10 02:07:00
太直接了,我放弃了- -
3 楼
yunzhou008 [专家分:410] 发布于 2006-06-10 06:55:00
呵呵 怎么会没有隐藏呢?
4 楼
yunzhou008 [专家分:410] 发布于 2006-06-10 06:57:00
IAkIAk你是我的偶像啊,捧捧场嘛
[em1]
5 楼
maobingwen [专家分:0] 发布于 2006-06-10 07:53:00
#include<iostream.h>
#include<conio.h>
#include<math.h>
#pragma argsused
struct point
{
int X;
int Y;
};
int main(int argc, char* argv[])
{ int b[1000],N,k(0);
point a[1000];
cout<<"测试数据数";
cin>>N;
for(int i=0;i<N;i++)
{
cin>>a[i].X;
cin>>a[i].Y;
}
for(int i=0;i<N;i++)
{
for(int j=i+1;j<=N-1;j++)
{
b[k]=sqrt((a[i].X-a[j].X)*(a[i].X-a[j].X)-(a[i].Y-a[j].Y)*(a[i].Y-a[j].Y));
k++;
}
}
double max=b[0];
for(int i=1;i<N;i++)
{
if(b[i]>max)
max=b[i];
}
cout<<max<<endl;
getch();
return 0;
}
6 楼
fenix124 [专家分:70] 发布于 2006-06-10 09:37:00
楼上的,都说了O(n^2)的不行了,数据规模为30000.
可能可以找到O(nlogn)的算法吧
7 楼
sllone [专家分:150] 发布于 2006-06-10 20:23:00
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
double count(int *a,int i,int j){
double tmp=sqrt(pow((a[i*2]-a[j*2]),2)+pow((a[i*2]-a[j*2]),2));
return tmp;
}
double deal(int *a,int size){
double maxd,tmp;
maxd=count(a,0,1);
for(int i=0;i<size;i++)
for(int j=i+1;j<size;j++){
tmp=count(a,i,j);
maxd=(maxd<tmp)?tmp:maxd;
}
return maxd;
}
void main(){
int size,*a;
scanf("%d",&size);
a=(int *)malloc((2*size)*sizeof(int));
for(int i=0;i<size;i++)
scanf("%d%d",&a[i*2],&a[i*2]);
printf("%.2f",deal(a,size));
}
8 楼
fantasyzzz [专家分:0] 发布于 2006-06-10 20:50:00
算法教材上有最近点对问题,不过我还是不知道怎么和最长点对问题联系起来,想了半天分治法也没有什么思路。。
9 楼
yxs0001 [专家分:9560] 发布于 2006-06-11 09:59:00
想出来 告诉你们 你们不要想了
开玩笑的
10 楼
shijizhisheng [专家分:180] 发布于 2006-06-11 11:58:00
#include <iostream>
#include <cmath>
using namespace std;
int main()
{ double max(float *);
int n,t;
cout<<"请输入点数n的值:";
cin>>n;
cout<<"请输入n个点的坐标(x,y):"<<endl;
float x,y,point[3000][2];
while(n--)
{
cin>>x>>y;
point[n][0]=x;
point[n][1]=y;
t=1;
}
if(t==1)cout<<"您的输入完毕!"<<endl;
max(point);
return 0;
}
double max(float *point)
{ int n;
double m[3000],t;
for(int i=0;i<n;i++)
for(int j=0;j<i;j++)
m[i]=sqrt((point[i][0]-point[j][0])*(point[i][0]-point[j][0])+(point[i][1]-point[j][1])*(point[i][1]-point[j][1]));
t=m[0];
for(i=0;i<3000;i++)
if(m[i]>t)t=m[i];
cout<<"点距最远为:"<<t<<endl;
}
我写的有点问题。希望各位高手来帮助我改一下;我的qq:563794874
邮箱为:shijizhisheng@126.com
我来回复