1. 程式人生 > >[BJWC2011]最小三角形

[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;
}