POJ3384 Feng Shui

嘟嘟嘟


昨天我看到的这道题,今天终于A了。
写这道题的时间其实并不长,主要是我为这题现学了一个半平面相交(虽然是(O(n ^ 2))的……)


思路说难也不难,关键是第一步的转化得想到。
首先可以肯定的是两圆要离得尽量远。
把每一条边向内移动(r)的距离,得到一个新的比原来小的凸包,那么这个凸包表示的是两个圆的圆心可以到达的地方。于是就转化成了求最远点对了。
向内移动(r)的距离我是用向量做的:对于边(AB),得到旋转(90)度后的向量(overrightarrow{AB'}),那么端点(A)旋转后的点(A' = A + overrightarrow{AB'} * ( frac{r}{| overrightarrow{AB'}|}))(B)同理。
然后对于新的边(A'B'),搞一次半平面相交即可。
然后最远点对就是套路了:跑一边凸包后搞一遍旋转卡壳,完事了。
坑点就是这题(spj)估计是小学生写的,一是取最大值的时候,如果当前值(geqslant)最大值也得更新;二是横坐标小的点得先输出……
还有就是求最远点对其实暴力就够了……

#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-10;
const int maxn = 1e3 + 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, cnt;
db r;
struct Point
{
  db x, y;
  Point operator - (const Point& oth)const
  {
    return (Point){x - oth.x, y - oth.y};
  }
  db operator * (const Point& oth)const
  {
    return x * oth.y - oth.x * y;
  }
  Point operator * (const db& d)const
  {
    return (Point){x * d, y * d};
  }
  friend inline Point rot(const Point& A)
  {
    return (Point){-A.y, A.x};
  }
  friend inline db dis(const Point& A)
  {
    return A.x * A.x + A.y * A.y;
  }
}p[maxn], a[maxn];

Point calc(Point N, Point A, Point B)
{
  Point AB = rot(B - A);
  return N - (AB * (-r / sqrt(dis(A - B))));
}

int tot = 0;
Point b[maxn];
db cross(Point C, Point A, Point B)
{
  return (B - A) * (C - A);
}
void addCross(Point A, Point B, Point C, Point D)
{
  db s1 = (C - A) * (D - A), s2 = (D - B) * (C - B);
  b[++tot] = A - (A - B) * (s1 / (s1 + s2));
}
void Cut(Point A, Point B)
{
  tot = 0;
  a[cnt + 1] = a[1];
  for(int i = 1; i <= cnt; ++i)
    {
      if(cross(a[i], A, B) < eps)
	{
	  b[++tot] = a[i];
	  if(cross(a[i + 1], A, B) > eps) addCross(A, B, a[i], a[i + 1]);
	}
      else if(cross(a[i + 1], A, B) < -eps) addCross(A, B, a[i], a[i + 1]);
    }
  for(int i = 1; i <= tot; ++i) a[i] = b[i];
  cnt = tot;
}

Point S;
int st[maxn], top = 0;
bool cmp(Point A, Point B)
{
  db s = (A - S) * (B - S);
  if(fabs(s) > eps) return s > eps;
  return dis(A - S) < dis(B - S) - eps;
}
void Graham()
{
  int id = 1; top = 0;
  for(int i = 2; i <= cnt; ++i)
    if(a[i].x < a[id].x - eps || (fabs(a[i].x - a[id].x) < eps && a[i].y < a[id].y - eps)) id = i;
  if(id != 1) swap(a[1].x, a[id].x), swap(a[1].y, a[id].y);
  S.x = a[1].x; S.y = a[1].y;
  sort(a + 2, a + cnt + 1, cmp);
  st[++top] = 1;
  for(int i = 2; i <= cnt; ++i)
    {
      while(top > 1 && (a[st[top]] - a[st[top - 1]]) * (a[i] - a[st[top - 1]]) < eps) top--;
      st[++top] = i;
    }
}

Point Ans1, Ans2;
int nxt(int x)
{
  if(++x > top) x = 1;
  return x;
}
db area(Point A, Point B, Point C)
{
  return fabs((B - A) * (C - A));
}   
void rota()
{
  db Max = 0;
  for(int i = 1, j = 3; i <= top; ++i)
    {
      while(nxt(j) != i && area(a[st[i]], a[st[i + 1]], a[st[j]]) < area(a[st[i]], a[st[i + 1]], a[st[nxt(j)]]) + eps) j = nxt(j);
      if(Max < dis(a[st[i]] - a[st[j]]) + eps)
	{
	  Max = dis(a[st[i]] - a[st[j]]);
	  Ans1 = a[st[i]], Ans2 = a[st[j]];
	}
      if(Max < dis(a[st[i + 1]] - a[st[j]]) + eps)
	{
	  Max = dis(a[st[i + 1]] - a[st[j]]);
	  Ans1 = a[st[i + 1]], Ans2 = a[st[j]];
	}
    }
}

int main()
{
  while(scanf("%d%lf", &n, &r) != EOF)
    {
      cnt = n;
      for(int i = 1; i <= n; ++i) p[i].x = read(), p[i].y = read();
      for(int i = 1; i <= n; ++i) a[i] = p[i];
      p[n + 1] = p[1];
      for(int i = 1; i <= n; ++i)
	{
	  Point A = calc(p[i], p[i + 1], p[i]), B = calc(p[i + 1], p[i + 1], p[i]);
	  Cut(A, B);
	}
      Graham(); st[top + 1] = st[1];
      rota();
      if(Ans1.x > Ans2.x + eps) swap(Ans1.x, Ans2.x), swap(Ans1.y, Ans2.y);
      printf("%.4f %.4f %.4f %.4f
", Ans1.x, Ans1.y, Ans2.x, Ans2.y);
    }
  return 0;
}
原文地址:https://www.cnblogs.com/mrclr/p/10007718.html