跳到正文

题解

P16393 Sol

OneLastNOIP约 16 分钟
原文

这是 2026年5月10日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年5月9日
收录
2026年5月10日
更新
2026年11月11日
篇幅
约 16 分钟
题目
P16393

版本

  1. v2当前
  2. v1

啥子做法。

校内重现做了这道,没想到转极坐标系,因为一万个细节没调出来。

首先二分答案,若答案 RR 满足条件,考虑什么样的点是可以到达的。容易发现若一个点 PP 在所有圆的并内,则 OPOP 也在所有圆的并内。则对于每个 ii,都有:(x−xi)2+(y−yi)2≤Ri2(x-x_i)^2+(y-y_i)^2\le R_i^2,其中 xx 和 yy 是答案对应的点的坐标,由于 xi2+yi2=Ri2x_i^2+y_i^2=R_i^2,化简可得 R2≤2xxi+2yyiR^2\le 2xx_i+2yy_i,这个限制显然对应一条直线,将直线分为三类:

  • yi=0y_i=0,是对 xx 进行限制,额外记录。
  • yi>0y_i>0,形如 y≥f(x)y\ge f(x)。
  • yi<0y_i<0,形如 y≤f(x)y\le f(x)。

考虑后两种分别合并起来代表什么限制。yi>0y_i>0 的限制对应一个下凸壳,而 yi<0y_i<0 的限制对应一个上凸壳,分别维护,可得合法的点集为一个凸包。此时若 RR 合法,则凸包内存在到原点距离为 RR 的点。

由于作者的计算几何知识为 ∅\varnothing,所以用的是以下做法:

  • 维护每个区间 [xl,xr][x_l,x_r],使得凸包的上下两半在区间内都是直线。
  • 则得到的点集为一个四边形。显然原点到四边形内一点的距离是连续的,因此只要求最大距离和最小距离。
  • 最大距离是容易的,因为取到最大距离的点必然是四个端点中的一个。
  • 最小距离则要在端点距离、可能存在的原点到四条边的垂线段长度间取较小值,且还要特判跨过坐标轴的情况。

于是就得到这份代码:

C++
# include <bits/stdc++.h>
# define rep(i, a, b) for(int i = (a);i <= (b);i ++)
# define lop(i, a, b) for(int i = (a);i <  (b);i ++)
# define dwn(i, a, b) for(int i = (a);i >= (b);i --)
# define int64 long long
# define ___main signed main()
# define f64 double
# define f32 float
# define f128 long double
# define int128 __int128
# define i128 ((__int128)1)
# define int32 signed
# define un unsigned
# define int16 short
# define int8 char
# define ret return
# define inl inline
# define reg register
# define fn void
# define elif else if
# define pb push_back
# define pp pop_back
using std::cout;
using std::cin;
# define CASES() int T;cin >> T;while(T--)
# define FaILurEg(s) freopen(s".in","r",stdin);freopen(s".out","w",stdout)
#if defined(_MSC_VER)
#  define FORCE_INLINE __forceinline
#else
#  define FORCE_INLINE __attribute__((always_inline)) inline
#endif
namespace packin {
template<typename T>struct _Arr {T *arr;int size;
FORCE_INLINE T& operator [](int i) {ret arr[i];}};
template<typename T>FORCE_INLINE fn read(T &x) {cin >> x;}
template<typename T>FORCE_INLINE fn read(_Arr<T>a) {rep(i, 1, a.size) cin >> a.arr[i];}
template<typename T>FORCE_INLINE fn read(T *a, int siz) {rep(i, 1, siz) cin >> a[i];}
template<typename T, typename... Args>FORCE_INLINE void read(T &x, Args&... args) {read(x);read(args...);}
inline void print_impl(const char* s) {while (*s) {cout << *s++;}}
template<typename T, typename... Args>inline void print_impl(const char* s, const T& x, const Args&... args) {while (*s) {if (s[0] == '{' && s[1] == '}') {cout << x;print_impl(s + 2, args...);return;}cout << *s++;}}
template<typename... Args>inline void print(const char* s, const Args&... args) {print_impl(s, args...);}
inline void error_impl(const char*s){while(*s)std::cerr<<*s++;}template<class T,class...A>inline void error_impl(const char*s,const T&x,const A&...a){while(*s){if(s[0]=='{'&&s[1]=='}'){std::cerr<<x;error_impl(s+2,a...);return;}std::cerr<<*s++;}}template<class...A>inline void error(const char*s,const A&...a){error_impl(s,a...);}
}
template<typename T>FORCE_INLINE T min(T a, T b) {ret (a < b)?a:b;}
template<typename T>FORCE_INLINE T max(T a, T b) {ret (a > b)?a:b;}
using namespace packin;
# define eps 0.000000000001L
# define double long double
using std::cout;
using std::cin;

int x[100005], y[100005];
int Q;
struct line{double k, b;double operator [](double x) {return k * x + b;}
   bool operator<(line b) {ret k < b.k;}
   line operator-(line B) {ret {k - B.k, b - B.b};}
}L[100005];
bool cmp(double a, double b){return fabs(a - b) <= eps;}
int n;

double Pt(line a, line b) {
    return (b.b - a.b) / (a.k - b.k);
}

bool check(double Rd) {
   std::vector<line> L1, L2;
   double mnX = -1e9, mxX = 1e9;
   L1.pb({0, -1e9L});
   L2.pb({0, 1e9L});
   rep(i, 1, n) if(y[i]){
      L[i].b = Rd / y[i] * Rd / 2;
      L[i].k = -x[i] * 1.0L / y[i];
      if(y[i] > 0)
         L1.pb(L[i]);//, std::cerr << "y >= ";
      else
         L2.pb(L[i]);//, std::cerr << "y <= ";
      //error("{}x + {}\n", L[i].k, L[i].b);
      }
   else {
      if(x[i] > 0)
         mnX = max(mnX, Rd * Rd / 2 / x[i]);
      else
         mxX = min(mxX, Rd * Rd / 2 / x[i]);
   }
   // L1  y >= f(x)
   // L2  y <= f(x)
   //error("x in [{},{}]\n", mnX, mxX);

   std::sort(L1.begin(), L1.end());
   std::sort(L2.begin(), L2.end());
   std::reverse(L2.begin(), L2.end());

   std::vector<std::pair<double, line> > $1, $2;
   for(auto l : L1) {
      if($1.empty())
         $1.pb({mnX, l});
      else {
         if($1.back().second.k == l.k && $1.back().second.b >= l.b)
            continue;
         while(!$1.empty() && (($1.back().second.k == l.k && $1.back().second.b < l.b) || Pt(l, $1.back().second) < $1.back().first))
            $1.pp();
         if($1.empty())
            $1.pb({mnX, l});
         else
            $1.pb({Pt(l, $1.back().second), l});
      }
   }

   for(auto l : L2) {
      if($2.empty())
         $2.pb({mnX, l});
      else {
         if($2.back().second.k == l.k && $2.back().second.b <= l.b)
            continue;
         while(!$2.empty() && ($2.back().second.k == l.k && $2.back().second.b > l.b) || Pt(l, $2.back().second) < $2.back().first)
            $2.pp();
         if($2.empty())
            $2.pb({mnX, l});
         else
            $2.pb({Pt(l, $2.back().second), l});
      }
   }
   
   if(mnX > mxX) ret false;

   std::set<double> pts;
   pts.insert(mnX);
   for(auto [p, l] : $1) pts.insert(p);
   for(auto [p, l] : $2) pts.insert(p);

   std::vector<std::tuple<double, line, line> >P;
   int p1 = 0, p2 = 0;
   for(auto p : pts) {
      while(p1 != $1.size() - 1 && $1[p1 + 1].first <= p)
         p1 ++;
      while(p2 != $2.size() - 1 && $2[p2 + 1].first <= p)
         p2 ++;
      P.pb(std::make_tuple(p, $1[p1].second, $2[p2].second));
      //std::cerr << p << " ";
      //error("[{}x + {}, {}x + {}]\n", $1[p1].second.k, $1[p1].second.b, $2[p2].second.k, $2[p2].second.b);
   }
   bool flg = 0;
   rep(i, 0, (int)P.size() - 1) {
      double R = ((i == P.size() - 1) ? mxX : std::get<0>(P[i + 1]));
      auto [L, ln, lx] = P[i];
      if((lx - ln)[R] < 0 && (lx - ln)[L] < 0)
         {continue;}
      if((lx - ln)[R] < 0)
         R = Pt(lx, ln);
      if((lx - ln)[L] < 0)
         L = Pt(lx, ln);
      double maxL = std::sqrtl(max(
         max(L * L + ln[L] * ln[L], L * L + lx[L] * lx[L]),
         max(R * R + ln[R] * ln[R], R * R + lx[R] * lx[R])
      ));
      line xn = {-1.0L/ ln.k, 0}, xx = {-1.0L / lx.k, 0};
      double minL = 1e9;
      if(ln.k != 0)
      if(Pt(xn, ln) <= R && L <= Pt(xn, ln))
         minL = std::sqrtl(Pt(xn, ln) * Pt(xn, ln) + xn[Pt(xn, ln)] * xn[Pt(xn, ln)])
         ; 
      //std::cerr << minL <<"#" << ln.k << " " << xn.k<< " "<< Pt(xn, ln) << " " << xn[Pt(xn, ln)] << " " << ln[Pt(xn, ln)] << "\n";
      if(lx.k != 0)
         if(Pt(xx, lx) <= R && L <= Pt(xx, lx))
            minL = min(minL, std::sqrtl(Pt(xx, lx) * Pt(xx, lx) + xx[Pt(xx, lx)] * xx[Pt(xx, lx)]));
         else;

      minL = min(minL,
         std::sqrtl(min(
            min(L * L + ln[L] * ln[L], L * L + lx[L] * lx[L]),
            min(R * R + ln[R] * ln[R], R * R + lx[R] * lx[R])
         ))
      );
      if(lx[R] >= 0 && ln[R] <= 0)
         minL = min(minL, (R < 0 ? -R : R));
      if(lx[L] >= 0 && ln[L] <= 0)
         minL = min(minL, (L < 0 ? -L : L));
      if(L <= 0 && R >= 0) {
         minL = min(minL, ln[0] < 0 ? -ln[0] : ln[0]);
         minL = min(minL, lx[0] < 0 ? -lx[0] : lx[0]);
         if(ln[0] <= 0 && lx[0] >= 0)
            minL = 0;
      }
      //std::cerr << minL << " " << maxL << "|" << Rd<< '\n';
      if(minL <= Rd && Rd <= maxL)
         ret true;
      flg = true;
   }
   //std::cerr << "\n\n True!\n";
   ret false;
} 

___main {std::ios::sync_with_stdio(0);cin.tie(0);
   int T;
   cin >> T;
   L[0].b=-100000000000000;
   while(T --) {
      cin >> n;
      rep(i, 1, n)
         cin >> x[i] >> y[i];
      double l = 0, r = 6e5, ans = 0;
      int cnt = 0;
      while(cnt <= 70) {
         double mid = (l + r) / 2;
         //error("{} {} {}CHK\n", l, r, mid);
         if(check(mid))
            ans = max(ans, mid), l = mid;
         else
            r = mid;
         cnt ++;
      }
      //check(10.2);
      cout<<std::fixed<<std::setprecision(10) << ans << "\n";
   }

   ret 0;}
/*
(x - x1)2 + (y - y1)2

x2 + y2 - 2xx1 - 2yy1 <= 0

check R :

R2 <= 2xx1 + 2yy1

y >= R2/2y1 - (x1/y1) x
(y1 >0)
y <= R2/2y1 - (x1/y1) x
(y1 < 0)

x12 + y12 = R2

*/