/*
translation:
有n个宝石,宝石为圆形。给出现在有根金属棒,靠近宝石距离m之内就会吸附上去。现在给出每颗宝石的信息,求用这个棒子一次
能钓出最多多少个宝石?
solution:
极限情况
每个宝石可以看成两个圆(本来的圆+半径加上m的圆)。对这些圆形一一枚举,即可求出各自的公切线(即金属棒)。然后
求出此时能吸附多少宝石。逐一更新即可。
note:
# 为何最后计算吸附多少宝石时用lamda表达式就能过,自己写的函数就过不了?QAQ
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 100 + 5;
const double PI = acos(-1);
struct Point
{
double x, y;
Point(){}
Point(double x_, double y_):x(x_),y(y_){}
};
typedef Point Vector;
int n;
struct Circle:public Point
{
Point c;
double r, m;
Circle(){}
Circle(Point c, double r, double m):c(c.x, c.y),r(r),m(m){}
Point getPoint(double a) {
return Point(c.x + cos(a) * r, c.y + sin(a) * r);
}
};
vector<Circle> p;
Vector operator + (Vector a, Vector b) { return Vector(a.x + b.x, a.y + b.y); }
Vector 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); }
bool operator < (const Point& a, const Point& b)
{
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
const double eps = 1e-12;
int dcmp(double x)
{
if(fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
bool operator == (const Point& a, const Point& b)
{
return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}
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 a, Vector b) { return acos(dot(a, b) / length(a) / length(b)); }
double angle(Vector v) { return atan2(v.y, v.x); }
double cross(Vector a, Vector b) { return a.x * b.y - b.x * a.y; }
double dist(Point p1,Point p2) { return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); }
double distToLine(Point p, Point a, Point b) {
Vector v1 = b - a, v2 = p - a;
return fabs(cross(v1, v2)) / length(v1);
}
int getTangents(Circle A, Circle B, vector<Point>& a, vector<Point>& b)
{
int cnt = 0;
if(A.r < B.r) { swap(A, B); swap(a, b); }
int d2 = (A.c.x - B.c.x)*(A.c.x - B.c.x) + (A.c.y - B.c.y)*(A.c.y - B.c.y);
int rdiff = A.r - B.r;
int rsum = A.r + B.r;
if(d2 < rdiff * rdiff) return 0;
double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x);
if(d2 == 0 && A.r == B.r) return -1;
if(d2 == rdiff * rdiff) {
a.push_back(A.getPoint(base));
b.push_back(B.getPoint(base));
return 1;
}
//有共切线
double ang = acos((A.r - B.r) / sqrt(d2));
a.push_back(A.getPoint(base + ang)); b.push_back(B.getPoint(base + ang));
a.push_back(A.getPoint(base - ang)); b.push_back(B.getPoint(base - ang));
if(d2 == rsum * rsum) {
a.push_back(A.getPoint(base));
b.push_back(B.getPoint(PI + base));
} else if(d2 > rsum * rsum) {
double ang = acos((A.r + B.r) / sqrt(d2));
a.push_back(A.getPoint(base + ang)); b.push_back(B.getPoint(PI + base + ang));
a.push_back(A.getPoint(base - ang)); b.push_back(B.getPoint(PI + base - ang));
}
return a.size();
}
int cmp(double a, double b)
{
const double diff = a - b;
if (fabs(diff) < eps) return 0;
else if (diff < 0) return -1;
else return 1;
}
int main()
{
//freopen("in.txt", "r", stdin);
while(~scanf("%d", &n)) {
if(n == 0) break;
p.clear();
double x, y, r, m;
for(int i = 0; i < n; i++) {
scanf("%lf%lf%lf%lf", &x, &y, &r, &m);
p.push_back(Circle(Point(x, y), r, m));
}
vector<Point> a, b;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) if(i != j) {
Circle A1 = p[i], A2 = p[i];
Circle B1 = p[j], B2 = p[j];
B2.r += B2.m; A2.r += A2.m;
getTangents(A1, B1, a, b);
getTangents(A1, B2, a, b);
getTangents(A2, B1, a, b);
getTangents(A2, B2, a, b);
}
}
int num = a.size(), ans = 1;
for(int i = 0; i < num; i++) {
int res = count_if(p.begin(), p.end(), [&](const Circle& C) {
return cmp(distToLine(C.c, a[i], b[i]), C.r) >= 0
&& cmp(distToLine(C.c, a[i], b[i]), C.r + C.m) <= 0; });
ans = max(ans, res);
}
printf("%d\n", ans);
}
return 0;
}
aoj2201(极限情况)
最新推荐文章于 2018-10-16 20:26:46 发布