2458: [BeiJing2011]最小三角形

2458: [BeiJing2011]最小三角形

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 1140   Solved: 391
[ Submit][ Status][ Discuss]

Description

Xaviera现在遇到了一个有趣的问题。
平面上有N个点,Xaviera想找出周长最小的三角形。
由于点非常多,分布也非常乱,所以Xaviera想请你来解决这个问题。
为了减小问题的难度,这里的三角形也包括共线的三点。

Input

第一行包含一个整数N表示点的个数。
接下来N行每行有两个整数,表示这个点的坐标。

Output

输出只有一行,包含一个6位小数,为周长最短的三角形的周长(四舍五入)。

Sample Input

4
1 1
2 3
3 3
3 4

Sample Output

3.414214


HINT

100%的数据中N≤200000。

Source

[ Submit][ Status][ Discuss]

采用分治计算
这个是计算平面最近点对的思路传送门
求三角形也差不多
设左右区间返回的区间最小值为D
根据三角形两边之和大于第三边
那么我们维护一个周长不超过D/2的正方形来统计就好
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;

typedef double DB;
const int maxn = 2E5 + 20;
const DB INF = 1E15;
const DB eps = 1E-9;
const DB four = 4.00;
const DB two = 2.00;

struct Point{
	DB x,y;
}p[maxn],now[maxn];

int n,tot;
DB po[maxn];

bool cmpx(const Point &a,const Point &b) {return a.x < b.x;}
bool cmpy(const Point &a,const Point &b) {return a.y < b.y;}

DB dis(Point A,Point B) 
{
	DB X = A.x - B.x,Y = A.y - B.y;	
	return sqrt(X*X + Y*Y);
}

DB Solve(int l,int r)
{
	if (r - l < 2) return INF;
	if (r - l == 2) return dis(p[l],p[l+1])+dis(p[l],p[r])+dis(p[l+1],p[r]);
	int mid = (l + r) >> 1;
	DB D1 = Solve(l,mid);
	DB D2 = Solve(mid+1,r);
	DB D = min(D1,D2),ret = D; tot = 0;
	for (int i = l; i <= r; i++) 
		if (fabs(p[i].x - p[mid].x) <= D/four)
			now[++tot] = p[i];
	
	sort(now + 1,now + tot + 1,cmpy);
	for (int i = 1; i <= tot; i++) 
		for (int j = i + 1; j <= tot; j++) {
			if (now[j].y - now[i].y > D/two + eps) break;
			for (int k = j + 1; k <= tot; k++) {
				if (now[k].y - now[i].y > D/two + eps) break;
				ret = min(ret,dis(now[i],now[j])+dis(now[i],now[k])+dis(now[j],now[k]));
			}
		}
	return ret;
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	cin >> n;
	for (int i = 1; i <= n; i++) scanf("%lf%lf",&p[i].x,&p[i].y);
	sort(p + 1,p + n + 1,cmpx);
	printf("%.6lf",Solve(1,n));
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值