POJ 3335 Rotating Scoreboard (半平面交模板)

[复制链接]
发表于 2026-1-25 02:03:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
This year, ACM/ICPC World finals will be held in a hall in form of a simple polygon. The coaches and spectators are seated along the edges of the polygon. We want to place a rotating scoreboard somewhere in the hall such that a spectator sitting anywhere on the boundary of the hall can view the scoreboard (i.e., his line of sight is not blocked by a wall). Note that if the line of sight of a spectator is tangent to the polygon boundary (either in a vertex or in an edge), he can still view the scoreboard. You may view spectator's seats as points along the boundary of the simple polygon, and consider the scoreboard as a point as well. Your program is given the corners of the hall (the vertices of the polygon), and must check if there is a location for the scoreboard (a point inside the polygon) such that the scoreboard can be viewed from any point on the edges of the polygon.
Input

The first number in the input line, T is the number of test cases. Each test case is specified on a single line of input in the form n x1 y1 x2 y2 ... xn yn where n (3 ≤ n ≤ 100) is the number of vertices in the polygon, and the pair of integers xi yi sequence specify the vertices of the polygon sorted in order.
Output

The output contains T lines, each corresponding to an input test case in that order. The output line contains either YES or NO depending on whether the scoreboard can be placed inside the hall conforming to the problem conditions.
Sample

Input:
  1. 2
  2. 4 0 0 0 1 1 1 1 0
  3. 8 0 0  0 2  1 2  1 1  2 1  2 2  3 2  3 0
复制代码
Output:
  1. YES
  2. NO
复制代码
模板:
  1. #include<iostream>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<cmath>
  5. using namespace std;
  6. const int N = 110;
  7. const double eps = 1e-10;
  8. int sign(double x)
  9. {
  10.         if (fabs(x) < eps) return 0;
  11.         return x > 0 ? 1 : -1;
  12. }
  13. int dcmp(double x, double y)
  14. {
  15.         if (fabs(x - y) < eps) return 0;
  16.         return x > y ? 1 : -1;
  17. }
  18. int n;
  19. struct Point
  20. {
  21.         double x, y;
  22.         Point(double x = 0, double y = 0) :x(x), y(y) { }
  23.         Point operator+ (Point t) { return Point(x + t.x, y + t.y); }
  24.         Point operator- (Point t) { return Point(x - t.x, y - t.y); }
  25.         Point operator* (double t) { return Point(x * t, y * t); }
  26.         Point operator/ (double t) { return Point(x / t, y / t); }
  27.         void read() { cin >> x >> y; }
  28. }P[N], ch[N];
  29. typedef Point Vector;
  30. struct Line
  31. {
  32.         Point p, q;
  33.         Vector v;
  34.         double deg;
  35.         Line() { }
  36.         Line(Point p, Point q) : p(p), q(q) { v = q - p; deg = atan2(v.y, v.x); }
  37. }L[N], q[N];
  38. double cross(Vector a, Vector b)
  39. {
  40.         return a.x * b.y - a.y * b.x;
  41. }
  42. double cross(Point A, Point B, Point C)
  43. {
  44.         return cross(B - A, C - A);
  45. }
  46. double dot(Vector a, Vector b)
  47. {
  48.         return a.x * b.x + a.y * b.y;
  49. }
  50. bool cmp(Line a, Line b)
  51. {
  52.         if (!dcmp(a.deg, b.deg)) return cross(a.v, b.q - a.p) > 0;
  53.         return a.deg < b.deg;
  54. }
  55. Point get_intersection(Point p, Vector v, Point q, Vector w)
  56. {
  57.         Vector u = p - q;
  58.         double t = cross(w, u) / cross(v, w);
  59.         Point b = v * t;
  60.         return p + b;
  61. }
  62. Point get_intersection(Line a, Line b)
  63. {
  64.         return get_intersection(a.p, a.v, b.p, b.v);
  65. }
  66. //判断b, c的交点是否在a右边
  67. //等于 = 证明经过该点,就要删掉 = 的点(本题依照题意不能删除过重复点的点)
  68. bool on_right(Line a, Line b, Line c)
  69. {
  70.         Point o = get_intersection(b, c);
  71.         return sign(cross(a.p, a.q, o)) </*=*/ 0;
  72. }
  73. int half_plane_intersection(int n, Point ch[])
  74. {
  75.         sort(L, L + n, cmp);
  76.         int hh = 0, tt = 0, cnt = 0;
  77.         for (int i = 0; i < n - 1; i++)
  78.         {
  79.                 if (!dcmp(L[i].deg, L[i + 1].deg)) continue;
  80.                 L[cnt++] = L[i];
  81.         }
  82.         L[cnt++] = L[n - 1];
  83.         for (int i = 0; i < cnt; i++)
  84.         {
  85.                 while (hh + 1 < tt && on_right(L[i], q[tt - 1], q[tt - 2])) tt--;
  86.                 while (hh + 1 < tt && on_right(L[i], q[hh], q[hh + 1])) hh++;
  87.                 q[tt++] = L[i];
  88.         }
  89.         while (hh + 1 < tt && on_right(q[hh], q[tt - 1], q[tt - 2])) tt--;
  90.         while (hh + 1 < tt && on_right(q[tt - 1], q[hh], q[hh + 1])) hh++;
  91.         q[tt] = q[hh];
  92.         cnt = 0;
  93.         for (int i = hh; i < tt; i++)
  94.                 ch[cnt++] = get_intersection(q[i], q[i + 1]);
  95.         return cnt;
  96. }
  97. bool check()   //判断输入的点是否为逆时针
  98. {
  99.         double ans = 0;
  100.         for (int i = 1; i < n - 1; i++)
  101.         {
  102.                 ans += cross(P[i] - P[0], P[i + 1] - P[0]);
  103.                 double a = cross(P[i] - P[0], P[i + 1] - P[0]);
  104.         }
  105.         return ans < 0;
  106. }
  107. int main()
  108. {
  109.         int T;
  110.         cin >> T;
  111.         while (T--)
  112.         {
  113.                 cin >> n;
  114.                 int m = 0;
  115.                 for (int i = 0; i < n; i++) P[i].read();
  116.                 if(check())
  117.                         for (int i = 0; i < n; i++) L[i] = Line(P[(i + 1) % n], P[i % n]);
  118.                 else
  119.                         for (int i = 0; i < n; i++) L[i] = Line(P[i % n], P[(i + 1) % n]);
  120.                 int cnt = half_plane_intersection(n, ch);
  121.                 if (cnt >= 3) puts("YES");
  122.                 else puts("NO");
  123.         }
  124.         return 0;
  125. }
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表