Fruit Slicer Kattis - fruitslicer(计算几何+思维)
程序员文章站
2022-03-02 10:55:48
...
题意
给出n个圆的圆心坐标,每个圆的半径为1,问一条直线最多可穿过多少个圆
题解
类似于标准化的思想,首先以一个圆为基准,将其平移到圆心位置,同样的其他圆也要在同方向移动相同位移,之后再在其他的圆中任意找一个圆,以原点为圆心,将其旋转到与y=±1相切,为保持圆的相对位置不变,其他圆也要旋转相同角度,然后只需枚举每个圆的水平切线,再依据每条切线判断穿过圆的个数就方便多了,若一个圆的水平切线不满足-1≤y≤1,则舍弃,否则保存,最后对切线依次判断即可
#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
#define _for(i, a, b) for(int i = a; i < b; ++i)
#define _rep(i, a, b) for(int i = a; i <= b; ++i)
#define closeIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug cout << "******************" << endl
#define FREE freopen("in.txt", "r", stdin)
#define FREO freopen("out.txt", "w", stdout)
#define ls l, m, rt << 1
#define rs m + 1, r, rt << 1 | 1
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef long double LD;
const int MAXN = 1e5 + 10;
const int MOD = 998244353;
const LD eps = 1e-10;
struct Point {
LD x, y;
Point(double x = 0, double y = 0) :x(x), y(y) {}
void input() { scanf("%Lf%Lf", &x, &y); }
};
typedef Point Vector;
Vector operator+ (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator- (Vector A, Vector B) { return Vector(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; }
int dcmp(LD x) { if (fabs(x) < eps) return 0; 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;
}
LD Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
LD Length(Vector A) { return sqrt(Dot(A, A)); }
LD Angle(Vector A, Vector B) {
if (dcmp(Length(B)) == 0) return 0;
return acos(Dot(A, B) / Length(A) / Length(B));
}
LD Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
LD Area2(Point A, Point B, Point C) { return Cross(B - A, C - A); }
Vector Rotate(Vector A, LD rad) {
return Vector(A.x*cos(rad) - A.y*sin(rad), A.x*sin(rad) + A.y*cos(rad));
}
Vector Normal(Vector A) {
LD L = Length(A);
return Vector(-A.y / L, A.x / L);
}
inline bool inside(LD x) {
return dcmp(x + 1) >= 0 && dcmp(x - 1) <= 0;
}
int n;
Point P[105], Q[105];
int ans = 0;
int main() {
scanf("%d", &n);
_for(i, 0, n) P[i].input();
if (n == 1) { printf("1\n"); return 0; }
if (n == 2) { printf("2\n"); return 0; }
sort(P, P + n);
_for(i, 0, n) {
Vector deltav(-P[i].x, -P[i].y);
_for(j, 0, n) P[j] = P[j] + deltav;
_for(j, i + 1, n) {
LD rad = Angle(Vector(1, 0), Vector(P[j]));
if (dcmp(Cross(Vector(1, 0), Vector(P[j]))) > 0) {
_for(k, 0, n) Q[k] = Rotate(Vector(P[k]), -rad);
}
else {
_for(k, 0, n) Q[k] = Rotate(Vector(P[k]), rad);
}
vector<LD> vec;
int cnt;
_for(k, 0, n) {
if (k == i || k == j) continue;
if (dcmp(fabs(Q[k].y) - 2) > 0) continue;
if (inside(Q[k].y + 1)) vec.push_back(Q[k].y + 1);
if (inside(Q[k].y - 1)) vec.push_back(Q[k].y - 1);
}
vec.push_back(1);
vec.push_back(-1);
_for(k, 0, vec.size()) {
int cnt = 2;
LD d = vec[k];
_for(t, 0, n) {
if (t == i || t == j) continue;
if (dcmp(d + 1 - Q[t].y) >= 0 && dcmp(d - Q[t].y - 1) <= 0) ++cnt;
}
ans = max(ans, cnt);
}
}
}
printf("%d\n", ans);
return 0;
}
上一篇: 扫描线+线段树