timus 1588. Jamaica URAL 解题报告 平面点距离和
给n个点,求这些点的距离和,注意,如果三点共线的话,不要重复计算啊! 也就是说多点共线的话只计算最长的两点距离就好了!
一种思想是,先求任意两点简单距离,再减去重复的即可,但是不没想到好的去重的方法!
另一种思想是,找到如果多点共线的话用O(n^3)的方法,找到最远的 两点,然后计算这两的距离就好了;注意同时对这两个最远点做好标记,防止下次还找到他们两个,重复计算的情况!
小媛的博客里貌似有更高效的做法……
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define FOR(i,s,t) for(int i=(s); i<(t); i++)
#define BUG puts("here!!!")
#define STOP system("pause")
#define file_r(x) freopen(x, "r", stdin)
#define file_w(x) freopen(x, "w", stdout)
#define INFK 1e20
#define EPS 1e-8
using namespace std;
bool dd(double x,double y) { return fabs( x - y ) < EPS;} // x == y
const int MAX = 310;
struct Point
{
int x, y;
void get()
{
scanf("%d%d", &x, &y);
}
}pot[310];
Point p[MAX];
bool a[MAX][MAX];
double disp2p(Point a,Point b) // a b 两点之间的距离
{
return sqrt( 1.0*( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) );
}
double cross(Point p,Point p1,Point p2)
{
return (p2.x-p.x)*(p1.y-p.y)-(p1.x-p.x)*(p2.y-p.y);
}
bool check(Point a, Point b)
{
if( a.x == b.x ) return a.y < b.y;
return a.x < b.x;
}
int n;
double ans=0;
int tot=0;
bool vis[310][310];
int main()
{
cin>>n;
for(int i=0;i<n;++i)
{
pot[i].get();
}
memset(vis,0,sizeof(vis));
int tmin,tmax;
for(int i=0;i<n;++i)
{
tot=0;
for(int j=i+1;j<n;++j)
{
tmin=i,tmax=j;
if(check(pot[j],pot[i]))
{
swap(tmin,tmax);
}
for(int k=0;k<n;++k)
{
if(i==k||j==k)continue;
if(cross(pot[tmin],pot[tmax],pot[k]))continue;
if(check(pot[k],pot[tmin]))
tmin=k;
else if(check(pot[tmax],pot[k]))
tmax=k;
}
if(vis[tmin][tmax])continue;
ans+=disp2p(pot[tmin],pot[tmax]);
vis[tmin][tmax]=1;
}
}
printf("%.0f\n",ans);
return 0;
}