1043: [HAOI2008]下落的圆盘
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1407 Solved: 591
[ Submit][ Status][ Discuss]
Description
有n个圆盘从天而降,后面落下的可以盖住前面的。求最后形成的封闭区域的周长。看下面这副图, 所有的红
色线条的总长度即为所求.
Input
第一行为1个整数n,N<=1000
接下来n行每行3个实数,ri,xi,yi,表示下落时第i个圆盘的半径和圆心坐标.
Output
最后的周长,保留三位小数
Sample Input
2
1 0 0
1 1 0
1 0 0
1 1 0
Sample Output
10.472
HINT
Source
【分析】
N^2判断+贪心线段覆盖
需要用到acos,atan2之类的函数(具体不解释咯)
ps:注意前面的圆被后面的圆完全覆盖的情况!
【代码】
//bzoj 1043 [HAOI2008]下落的圆盘
#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm>
#define eps 1e-8
#define pi acos(-1)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=1005;
double ans;
int n,m,top;
struct angle {double l,r;} s[mxn];
struct circle {double r,x,y;} c[mxn];
inline bool comp(angle x,angle y)
{
if(x.l==y.l) return x.r<y.r;
return x.l<y.l;
}
inline double dis(circle c1,circle c2)
{
return sqrt((c1.x-c2.x)*(c1.x-c2.x)+(c1.y-c2.y)*(c1.y-c2.y));
}
inline bool contain(circle c1,circle c2)
{
return dis(c1,c2)+c2.r<=c1.r || c1.r+c2.r<=dis(c1,c2);
}
int main()
{
int i,j;
scanf("%d",&n);
fo(i,1,n)
scanf("%lf%lf%lf",&c[i].r,&c[i].x,&c[i].y);
fo(i,1,n)
{
fo(j,i+1,n) if(dis(c[i],c[j])+c[i].r<=c[j].r) break;
if(j!=n+1) continue;
top=0;
fo(j,i+1,n)
{
if(contain(c[i],c[j])) continue;
double k=atan2(c[j].y-c[i].y,c[j].x-c[i].x);
double A=dis(c[i],c[j]),B=c[i].r,C=c[j].r;
double ang=acos((A*A+B*B-C*C)/(2*A*B));
double l1=k-ang,l2=k+ang;
if(l1<0) l1+=2*pi;
if(l2<0) l2+=2*pi;
if(l1<=l2) s[++top]=(angle){l1,l2};
else s[++top]=(angle){l1,2*pi},s[++top]=(angle){0,l2};
}
sort(s+1,s+top+1,comp);
double now=0,tmp=0;
fo(j,1,top)
{
double t1=s[j].l,t2=s[j].r;
if(t1>now) tmp+=t1-now,now=t2;
else now=max(now,t2);
}
tmp+=2*pi-now;
ans+=tmp*c[i].r;
}
printf("%.3lf\n",ans);
return 0;
}