LA 2572 (求可见圆盘的数量) Kanazawa

题意:

把n个圆盘依次放到桌面上,按照放置的先后顺序给出这n个圆盘的圆心和半径,输出有多少个圆盘可见(即未被全部覆盖)。

分析:

题中说对输入数据进行微小扰动后答案不变。

所露出的部分都是由若干小圆弧组成的。因此求出每个圆与其他圆相交的小圆弧,取圆弧的终点,分别向里和向外移动一个很小的距离的到P'。标记包含P'的最上面的那个圆为可见。

 //#define LOCAL
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std; const int maxn = ;
const double PI = acos(-1.0);
const double Two_PI = PI * ;
const double eps = * 1e-;
int n;
double radius[maxn];
bool vis[maxn]; int dcmp(double x)
{
if(fabs(x) < eps) return ;
else return x < ? - : ;
} struct Point
{
double x, y;
Point(double x=, double y=):x(x), y(y) {}
};
typedef Point Vector;
Point operator + (Point A, Point B) { return Point(A.x+B.x, A.y+B.y); }
Point operator - (Point A, Point B) { return Point(A.x-B.x, A.y-B.y); }
Vector operator * (Vector A, double p){ return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p){ return Vector(A.x/p, A.y/p); }
Point center[maxn]; double Dot(Vector A, Vector B){ return A.x*B.x + A.y*B.y; }
double Length(Vector A){ return sqrt(Dot(A, A)); }
double angle(Vector v) { return atan2(v.y, v.x); }
double NormalizeAngle(double rad)
{//将所求角度化到0到2π之间
return rad - Two_PI*(floor(rad/Two_PI));
} void getCCIntersection(Point c1, double r1, Point c2, double r2, vector<double>& rad)
{
double d = Length(c1 - c2);
if(dcmp(d) == ) return;
if(dcmp(d-r1-r2) > ) return;
if(dcmp(d-fabs(r1-r2)) < ) return; double a = angle(c2-c1);
double ag = acos((r1*r1+d*d-r2*r2)/(*r1*d));
rad.push_back(NormalizeAngle(a + ag));
rad.push_back(NormalizeAngle(a - ag));
} int topmost(Point P)
{
for(int i = n-; i >= ; --i)
if(Length(P-center[i]) < radius[i]) return i;
return -;
} int main(void)
{
#ifdef LOCAL
freopen("2572in.txt", "r", stdin);
#endif while(scanf("%d", &n) == && n)
{
for(int i = ; i < n; ++i)
scanf("%lf%lf%lf", &center[i].x, &center[i].y, &radius[i]);
memset(vis, , sizeof(vis)); for(int i = ; i < n; ++i)
{
vector<double> rad;
rad.push_back(0.0);
rad.push_back(Two_PI);
for(int j = ; j < n && j != i; ++j)
getCCIntersection(center[i], radius[i], center[j], radius[j], rad);
sort(rad.begin(), rad.end()); for(int j = ; j < rad.size() - ; ++j)
{
double mid = (rad[j] + rad[j+]) / 2.0;
for(int side = -; side <= ; side += )
{
double r = radius[i] + side*eps;
int t = topmost(Point(center[i].x+r*cos(mid), center[i].y+r*sin(mid)));
if(t >= ) vis[t] = true;
}
}
}
int ans = ;
for(int i = ; i < n; ++i) if(vis[i]) ++ans;
printf("%d\n", ans);
} return ;
}

代码君

上一篇:Gamit的安装


下一篇:HDU 4757 Tree 可持久化字典树 trie