简单题,开始还以为是最小生成树呢。。
任意两点之间都有一条线段相连,如果三个点在一条线段上,那么不需要重复连接,最长那个即可。求最小的长度。也就是去掉重复的就行了。
最好想的就是O(n^3)算法了,直接枚举,然后找共线上最远的两个点,标记,求距离。下次找,如果在这条线段上的点,一定还能找到最远的已经标记的那俩点,找到后判断是否被标记即可。
讨论里有人讨论这题的最忧算法,有N^2logN的,我想了下,写了。
以每个点极角排序,然后扫描,二分查找一个点对称的最远点(如果没有的话,就是基准点),然后标记求长度即可。排序是极角相同的话,按长度从大到小排序了。
比上个算法快了点,但是还是离0.015很远。。。可能用double 还有 eps 计算量大吧。。唯一记住的是,atan2范围是(-3.14, 3.14],这个总记不住。。
我的N^3算法没有用double,就最后计算长度用double了。
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)
using namespace std;
const int MAX = 310;
struct point
{
int x, y;
void get()
{
scanf("%d%d", &x, &y);
}
};
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 ) );
}
int crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向 顺时针是正
{
return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}
bool check(point a, point b)
{
if( a.x == b.x ) return a.y < b.y;
return a.x < b.x;
}
int main()
{
int n, tmin, tmax;
double sum = 0.0;
memset(a, false, sizeof(a));
scanf("%d", &n);
FOR(i, 0, n)
p[i].get();
FOR(i, 0, n)
FOR(k, i+1, n)
{
tmin = i;
tmax = k;
if( check(p[k], p[i]) )
swap(tmin, tmax);
FOR(j, 0, n)
{
if( j == i || j == k ) continue;
if( crossProduct(p[i], p[k], p[j]) == 0 )
{
if( check(p[j], p[tmin]) )
tmin = j;
if( check(p[tmax], p[j]) )
tmax = j;
}
}
if( a[tmin][tmax] ) continue;
sum += disp2p(p[tmin], p[tmax]);
a[tmin][tmax] = a[tmax][tmin] = true;
}
printf("%.0lf\n", sum);
return 0;
}
O(n^2logn):
#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)
using namespace std;
const int MAX = 310;
const double eps = 1e-8;
const double pi = acos(-1.0);
bool dy(double x,double y) { return x > y + eps;} // x > y
bool xy(double x,double y) { return x < y - eps;} // x < y
bool dyd(double x,double y) { return x > y - eps;} // x >= y
bool xyd(double x,double y) { return x < y + eps;} // x <= y
bool dd(double x,double y) { return fabs( x - y ) < eps;} // x == y
struct point
{
int x, y;
void get()
{
scanf("%d%d", &x, &y);
}
};
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 ) );
}
int crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向 顺时针是正
{
return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}
bool check(point a, point b)
{
if( a.x == b.x ) return a.y < b.y;
return a.x < b.x;
}
point P;
struct NODE{ double ang; int id;};
NODE node[MAX];
bool cmp(NODE a,NODE b)
{
if( dd(a.ang, b.ang) )
return dy(disp2p(p[a.id], P), disp2p(p[b.id], P));
return xy(a.ang, b.ang);
}
bool cmp1(NODE a, NODE b)
{
return xy(a.ang, b.ang);
}
int main()
{
int n;
double sum = 0.0;
memset(a, false, sizeof(a));
scanf("%d", &n);
FOR(i, 0, n)
p[i].get();
FOR(i, 0, n)
{
P = p[i];
FOR(k, 0, n)
{
node[k].ang = atan2(0.0+p[k].y - p[i].y, 0.0+p[k].x - p[i].x);
node[k].id = k;
}
sort(node, node+n, cmp);
FOR(k, 0, n)
{
if( node[k].id == i ) continue;
NODE t;
t.ang = node[k].ang > 0 ? node[k].ang - pi : node[k].ang + pi;
int u = lower_bound(node, node+n, t, cmp1) - node;
int uu, kk = node[k].id;
if( u != n && dd(node[u].ang, t.ang) )
uu = node[u].id;
else
uu = i;
int tmpk = kk;
while( !crossProduct(p[i], p[tmpk], p[node[k+1].id]) && k + 1 < n )
k++;
if( a[uu][kk] ) continue;
a[uu][kk] = a[kk][uu] = true;
sum += disp2p(p[uu], p[kk]);
}
}
printf("%.0lf\n", sum);
return 0;
}