2458: [BeiJing2011]最小三角形
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 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
1 1
2 3
3 3
3 4
Sample Output
3.414214
HINT
100%的数据中N≤200000。
Source
#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;
}