[BJWC2011]最小三角形

嘟嘟嘟


这一看就是平面分治的题,所以就想办法往这上面去靠。
关键就是到\(mid\)点的限制距离是什么。就是对于当前区间,所有小于这个距离的点都选出来,参与更新最优解。
假设从左右区间中得到的最优解是\(d\),那么这个限制距离就是\(\frac{d}{2}\)。这很显然,如果三角形的一条边比\(\frac{d}{2}\)还大,那么他的周长一定大于\(d\)
因此我们选出所有小于\(\frac{d}{2}\)的点,然后比较暴力的更新答案,具体看代码。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 2e5 + 5;
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}

int n;
struct Point
{
  db x, y;
  bool operator < (const Point& oth)const
  {
    return x < oth.x;
  }
  Point operator - (const Point& oth)const
  {
    return (Point){x - oth.x, y - oth.y};
  }
  friend inline db dis(const Point& A)
  {
    return sqrt(A.x * A.x + A.y * A.y);
  }
}p[maxn], b[maxn], c[maxn], tp[maxn];

bool cmpy(Point a, Point b) {return a.y < b.y;}

db solve(int L, int R)
{
  if(L == R - 1) return INF;
  if(L == R - 2) return dis(p[L] - p[L + 1]) + dis(p[L + 1] - p[R]) + dis(p[R] - p[L]);
  int mid = (L + R) >> 1, cnt = 0;
  db d = min(solve(L, mid), solve(mid, R));
  db lim = d / 2;
  for(int i = L; i <= R; ++i)
    if(abs(p[i].x - p[mid].x) <= lim) tp[++cnt] = p[i];
  sort(tp + 1, tp + cnt + 1, cmpy);
  for(int i = 1, j = 1; i <= cnt; ++i)
    {
      for(; j <= cnt && abs(tp[j].y - tp[i].y) <= lim; ++j);
      for(int k = i + 1; k < j; ++k)
    for(int l = i + 1; l < k; ++l)
      d = min(d, dis(tp[i] - tp[k]) + dis(tp[k] - tp[l]) + dis(tp[i] - tp[l]));
    }
  return d;
}

int main()
{
  n = read();
  for(int i = 1; i <= n; ++i) p[i].x = read(), p[i].y = read();
  sort(p + 1, p + n + 1);
  printf("%.6lf\n", solve(1, n));
  return 0;
}

猜你喜欢

转载自www.cnblogs.com/mrclr/p/10031262.html
今日推荐