题目链接https://nanti.jisuanke.com/t/A1993
题意
平面上给出若干个半径相同且互不相交的n个小圆,问能够包含s个小圆的大圆半径最小是多少
题解
标准做法:二分答案 + 最多点覆盖
复杂度:
O
(
(
l
o
g
D
e
p
s
n
2
l
o
g
n
)
)
O((log\frac{D}{eps} n^2logn))
O((logepsDn2logn))
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
const int N=4e5+7;
const db eps=1e-8;
const db inf=1e4;
const db pi=acos(-1.0);
int t,n,s;
db rr;
int sign(db k){if(k>eps) return 1;if(k<-eps) return -1;return 0;}
int dcmp(db k1,db k2){return sign(k1-k2);}
struct Point{
db x,y;
Point operator - (const Point k)const{return (Point){x-k.x,y-k.y};}
bool operator == (const Point k)const{return dcmp(x,k.x)==0&&dcmp(y,k.y)==0;}
db abs2(){return x*x+y*y;}
db abs(){return sqrt(abs2());}
db dis2(Point k){return ((*this)-k).abs2();}
db dis(Point k){return sqrt(dis2(k));}
void input(){scanf("%lf%lf",&x,&y);}
void output(){printf("(%f,%f)\n",x,y);}
}p[N];
struct Node{
db ang;
int flag;
bool operator<(const Node k)const{
if(dcmp(ang,k.ang)==0) return flag>k.flag;
return ang<k.ang;
}
}q[N];
bool ck(db r){
int res=1;
for(int i=1;i<=n;i++){
int tot=0;
int sum=1;
for(int j=1;j<=n;j++){
if(i==j) continue;
db d=p[i].dis(p[j]);
if(dcmp(d,2*r)>0) continue;
if(sign(d)==0){sum++;continue;}
db th=atan2(p[j].y-p[i].y,p[j].x-p[i].x);
if(th<0) th+=2*pi;
db ph=acos(d/2.0/r);
q[++tot]=(Node){th-ph+2*pi,1};
q[++tot]=(Node){th+ph+2*pi,-1};
}
sort(q+1,q+1+tot);
for(int j=1;j<=tot;j++) res=max(res,sum+=q[j].flag);
}
return res>=s;
}
int main()
{
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&s);
for(int i=1;i<=n;i++) p[i].input();
scanf("%lf",&rr);
if(n<s){printf("The cake is a lie.\n");continue;}
db lo=0,hi=inf;
while(hi-lo>eps){
db mid=(lo+hi)/2;
if(ck(mid)) hi=mid;
else lo=mid;
}
printf("%.4f\n",lo+rr);
}
}
有一个超级神奇的优化
先枚举点,然后再二分,这样可以做到剪枝。
如果当前点在此时答案已经不满足的情况下,就跳过。
期望的复杂度是:
O
(
n
2
l
o
g
(
n
)
+
n
(
l
o
g
(
n
)
)
2
l
o
g
(
D
e
p
s
)
)
O(n^2log(n)+n(log(n))^2log(\frac{D}{eps}))
O(n2log(n)+n(log(n))2log(epsD))
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
const int N=4e5+7;
const db eps=1e-8;
const db inf=1e4;
const db pi=acos(-1.0);
int t,n,s;
db rr;
int sign(db k){if(k>eps) return 1;if(k<-eps) return -1;return 0;}
int dcmp(db k1,db k2){return sign(k1-k2);}
struct Point{
db x,y;
Point operator - (const Point k)const{return (Point){x-k.x,y-k.y};}
bool operator == (const Point k)const{return dcmp(x,k.x)==0&&dcmp(y,k.y)==0;}
db abs2(){return x*x+y*y;}
db abs(){return sqrt(abs2());}
db dis2(Point k){return ((*this)-k).abs2();}
db dis(Point k){return sqrt(dis2(k));}
void input(){scanf("%lf%lf",&x,&y);}
void output(){printf("(%f,%f)\n",x,y);}
}p[N];
struct Node{
db ang;
int flag;
bool operator<(const Node k)const{
if(dcmp(ang,k.ang)==0) return flag>k.flag;
return ang<k.ang;
}
}q[N];
bool ck(int i,db r){
int res=1;
int tot=0;
int sum=1;
for(int j=1;j<=n;j++){
if(i==j) continue;
db d=p[i].dis(p[j]);
if(dcmp(d,2*r)>0) continue;
if(sign(d)==0){sum++;continue;}
db th=atan2(p[j].y-p[i].y,p[j].x-p[i].x);
if(th<0) th+=2*pi;
db ph=acos(d/2.0/r);
q[++tot]=(Node){th-ph+2*pi,1};
q[++tot]=(Node){th+ph+2*pi,-1};
}
sort(q+1,q+1+tot);
for(int j=1;j<=tot;j++) res=max(res,sum+=q[j].flag);
return res>=s;
}
int main()
{
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&s);
for(int i=1;i<=n;i++) p[i].input();
scanf("%lf",&rr);
db ans=0;
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) ans=max(ans,p[i].dis(p[j]));
if(n<s){printf("The cake is a lie.\n");continue;}
for(int i=1;i<=n;i++){
if(ck(i,ans)){
db lo=0,hi=ans;
while(hi-lo>eps){
db mid=(lo+hi)/2;
if(ck(i,mid)) hi=mid;
else lo=mid;
}
ans=lo;
}
}
printf("%.4f\n",ans+rr);
}
}