ACM题(凸包),为什么两端代码差不多却 时间差距这么大?

welon123 2010-11-22 08:38:08
http://acm.hdu.edu.cn/showproblem.php?pid=2202

为什么下边两段代码,第一段是别人的,提交只有31MS,我的却是2000MS,超时
即使我改成按海伦公式算面积,仍然是2000,时间差距主要在哪里呀
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;
typedef struct
{
int x;
int y;
}point;

point result[50001];
point m[50001];
int n,top;

double dis(point a, point b)
{
double distance;

distance = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
distance = sqrt(distance);

return distance;
}

double Multiply(point a, point b, point c)
{
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);//计算叉积
}

int cmp(const void *a, const void *b)
{
point *c, *d;
double t;

c = (point *)a;
d = (point *)b;
t = Multiply(m[0],*c,*d);
if(t < 0)
return 1;
else if(t == 0 && (dis(m[0],*c) < dis(m[0],*d)))
return 1;
else
return -1;
}

void tubao()
{
int i;
result[0] = m[0];
result[1] = m[1];
result[2] = m[2];
top = 2;
for(i = 3; i <= n; i ++)
{
while(Multiply(result[top - 1],result[top], m[i]) <= 0 && top >= 2)//叉积为负 向右拐 即凹;
{ //叉积为正 向左拐 即凸.
top --;
}
result[++ top] = m[i];
}
}

int main()
{
int i,j,k,px,py,p;
point temp;
double a,b,c,s;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%d%d",&m[i].x,&m[i].y);
if(i == 0 || m[i].y < py)
{
px = m[i].x;
py = m[i].y;
p = i;
}
else if(m[i].y == py && m[i].x < px)
{
px = m[i].x;
py = m[i].y;
p = i;
}
}
temp=m[0];m[0]=m[p];m[p]=temp;
qsort(m+1,n-1,sizeof(m[0]),cmp);
m[n]=m[0];
tubao();
double area[50001];
double max =0;
for(i=0;i<top;i++) //求面积
{
for(j=i+1;j<top;j++)
{
for(k=j+1;k<top;k++)
{
a = dis(result[i],result[j]);//海伦公式
b = dis(result[i],result[k]);
c = dis(result[j],result[k]);
s=(a+b+c)/2;
a=s-a;
b=s-b;
c=s-c;
area[i]=s*a*b*c;
area[i]=sqrt(area[i]);
if(max<area[i])
max=area[i];
}
}
}
printf("%.2lf\n",max);
}
return 0;
}



我的代码:
#include<iostream>
#include<cmath>
#define SIZE 50010
using namespace std;

int N;
typedef struct
{
double x;
double y;
}Point;
Point p[SIZE], stack[SIZE];
int top;

double Mul(Point p1, Point p2, Point p3)
{
return (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x);
}

double Dis(Point a, Point b)
{
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

int cmp(const void *a, const void *b)
{
Point c = *(Point *)a;
Point d = *(Point *)b;
double k = Mul(p[0], c, d);
if(k < 0 || (!k && Dis(c, p[0]) > Dis(d, p[0])))
return 1;
return -1;
}

void Convex()
{
for(int i = 1; i < n; i++)
{
Point temp;
if(p[i].y < p[0].y || (p[i].y == p[0].y && p[i].x < p[0].x))
{
temp = p[i];
p[i] = p[0];
p[0] = temp;
}
}

qsort(p + 1, n - 1, sizeof(p[0]), cmp);

stack[0] = p[0];
stack[1] = p[1];
stack[2] = p[2];
top =2;
for(int i = 3; i < n; i++)
{
while(top >= 2 && Mul(stack[top - 1], stack[top], p[i]) <= 0)
top--;
top++;
stack[top] = p[i];
}
}

int main()
{
int i, j, k;
while(cin >> N, N){
for(i = 0; i < N; i++)
cin >> p[i].x >> p[i].y;

Convex();

double max_area = 0;
double m;
for(i = 0; i <= top - 2 ; i++)
{
for(j = i + 1; j <= top - 1; j++)
for(k = j + 1; k <= top; k++)
{
m = fabs(Mul(stack[i], stack[j], stack[k]) / 2);
max_area = m > max_area ? m : max_area;
}
}
cout.setf(ios::fixed);
cout.precision(2);
cout << max_area << endl;
}
return 0;
}




...全文
125 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
fire_woods 2010-11-23
  • 打赏
  • 举报
回复
typedef struct
{
double x;
double y;
}Point;

改成
typedef struct
{
int x;
int y;
}Point;
试试吧
FancyMouse 2010-11-23
  • 打赏
  • 举报
回复
50000的规模写3次方?
welon123 2010-11-23
  • 打赏
  • 举报
回复
我勒个去,终于找到原因了把
while(cin >> N, N)
改成
while(cin >> N)
就正常了
welon123 2010-11-23
  • 打赏
  • 举报
回复
楼上的,改了还是超时呀…… 我觉得时间应该差不多呀,却超了那么多
w251988889 2010-11-22
  • 打赏
  • 举报
回复
自己去比较..这么长代码..
自己耐心点..里面有很多相同的过程..都一样..看哪不同就知道了..这么没耐心.
  • 打赏
  • 举报
回复
搞ACM输入输出建议还是用C的吧

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧