[POJ3743]LL’s Cake

传说中的切糕(切蛋糕)!出题人的题解:http://hi.baidu.com/xh176233756/item/cac0c6f8f9f1ec19e3e3bd3d

其实比较麻烦的是求出每块多边形的面积,如果能做出来的话,这题只是再略微加上一个弓形。下面先讲一下求每块多边形面积的方法:求域法

求域

首先预处理,算出所有交点,然后每个点向它相邻(指有线相连)的所有点连边:

cake1

枚举每一条边(u, v),找出以v为起点,和(v, u)相比逆时针转角最大的边,按那条边走过去。重复这个步骤,直到走到了最初的那个点。经过的边围成的域就是一个独立的块:

cake5

cake2

cake6

cake4

求弓形面积

把弧定义成一种特殊的边!

具体实现

定义Segment类型为边,并且用Segment::arc == 1代表这是一条弧。

首先算出直线跟圆的所有交点,按照逆时针顺序,连单向弧(i, (i+1)%(2n))。然后对于每条直线,枚举所有直线和它求交点,把交点按(x, y)双关键字排序,连双向边(i, i+1)。接着对每个点,以它为中心,把它相连的所有边极角排序。最后枚举每条边,以它为起始边求域。

求域的方法:如果一条边(u, v)是弧,加上弓形面积。逆时针枚举和v相连的所有边,直到枚举到(v, u),把(v, u)的前一条边作为下一次要走的边。不断执行上述过程,记下走过的所有点,最后计算围成的多边形的面积。

还有,实现的时候因为要给点编号,为了避免相同点重复编号,要给点hash。

代码

十分感谢出题人。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using std::vector;

inline int sign(const double x)
{
  static const double EPS = 1e-8;
  if (x > EPS) return 1;
  return x < -EPS ? -1 : 0;
}
const double PI = std::acos(-1.0);
const int MAXN = 200;
const int HASHMOD = 65497;
const int SEED = 57123577;
int Testcase, n, cnt, hashid[65536], hashpos[MAXN*MAXN];
struct Point
{
  double x, y;
  Point() { }
  Point(const double a, const double b): x(a), y(b) { }
};
const Point EMPTY(-10086, -12580);
Point O, hash[65536];
inline bool operator!=(const Point& lhs, const Point& rhs) { return sign(lhs.x-rhs.x) || sign(lhs.y-rhs.y); }
inline Point operator-(const Point& lhs, const Point& rhs) { return Point(lhs.x-rhs.x, lhs.y-rhs.y); }
inline bool operator<(const Point& lhs, const Point& rhs) { return sign(lhs.x-rhs.x) < 0 || (sign(lhs.x-rhs.x) == 0 && sign(lhs.y-rhs.y) < 0); }
inline double cross(const Point& A, const Point& B, const Point& C, const Point& D)
{
  const double x1 = B.x-A.x, y1 = B.y-A.y;
  const double x2 = D.x-C.x, y2 = D.y-C.y;
  return x1*y2 - x2*y1;
}
int intersection(const Point& p1, const Point& p2, const Point& p3, const Point& p4, Point& p)
{
  const double s1 = cross(p1, p3, p1, p4);
  const double s2 = cross(p2, p3, p2, p4);
  const double s3 = cross(p3, p1, p3, p2);
  const double s4 = cross(p4, p1, p4, p2);
  if (s1*s2 < 0 && s3*s4 < 0)
  {
    p.x = (s1*p2.x - s2*p1.x) / (s1 - s2);
    p.y = (s1*p2.y - s2*p1.y) / (s1 - s2);
    return 5;
  }
  return 0;
}
inline bool cmp(const Point& lhs, const Point& rhs) { return std::atan2(lhs.y, lhs.x) < std::atan2(rhs.y, rhs.x); }
inline int locate(const Point& p)
{
  const long long x = static_cast<long long>(p.x * 1e8);
  const long long y = static_cast<long long>(p.y * 1e8);
  int h = ((x * SEED + y) % HASHMOD + HASHMOD) % HASHMOD;
  while (hash[h] != EMPTY && hash[h] != p) ++h;
  return h;
}
inline int getid(const Point& p)
{
  const int h = locate(p);
  if (hashid[h] != -1) return hashid[h];
  hash[h] = p, hashpos[cnt] = h;
  return hashid[h] = cnt++;
}

struct Segment
{
  bool arc, vis;
  int target;
  //Segment() { }
  Segment(const int v, const bool isarc): arc(isarc), vis(false), target(v) { }
};
vector<Segment> v[MAXN*MAXN];
void addedge(const Point& x, const Point& y, const bool isarc)
{
  const int a = getid(x), b = getid(y);
  v[a].push_back(Segment(b, isarc));
  if (!isarc) v[b].push_back(Segment(a, isarc));
}
inline double arcarea(const Point& a, const Point& b)
{
  double angle = - std::atan2(a.y, a.x) + std::atan2(b.y, b.x);
  if (sign(angle) < 0) angle += 2.0*PI;
  return 50.0 * (angle - std::sin(angle));
}
inline bool cmp2(const Segment& lhs, const Segment& rhs)
{
  if (lhs.target == rhs.target) return (lhs.arc ? 1 : 0) > (rhs.arc ? 1 : 0);
  return cmp(hash[hashpos[lhs.target]]-O, hash[hashpos[rhs.target]]-O);
}

double go(int st, vector<Segment>::iterator edge)
{
  const int first = st;
  vector<int> p;
  double res = .0;
  do
  {
    const int target = edge->target;
    if (edge->arc)
      res += arcarea(hash[hashpos[st]], hash[hashpos[target]]);
    edge->vis = true;
    p.push_back(st);
    for (vector<Segment>::iterator e = v[target].begin(); e != v[target].end(); ++e)
      if (edge->arc == e->arc && (edge->arc || e->target == st))
      {
        edge = e == v[target].begin() ? v[target].end() : e;
        --edge;
        break;
      }
    st = target;
  }
  while (st != first);

  p.push_back(first);
  for (vector<int>::size_type i = 1; i < p.size(); ++i)
    res += cross(O, hash[hashpos[p[i-1]]], O, hash[hashpos[p[i]]]) / 2.0;

  return res;
}

double solve()
{
  static Point p[MAXN*MAXN], cut[MAXN][2], tmp;
  memset(hashid, 0xFF, sizeof(hashid));
  memset(hashpos, 0xFF, sizeof(hashpos));
  std::fill(hash, hash+65536, EMPTY);
  cnt = 0;

  for (int i = 0; i < n; ++i)
  {
    double a, b;
    scanf("%lf%lf", &a, &b);
    cut[i][0].x = 10.0 * std::cos(a), cut[i][0].y = 10.0 * std::sin(a);
    cut[i][1].x = 10.0 * std::cos(b), cut[i][1].y = 10.0 * std::sin(b);
    p[2*i] = cut[i][0], p[2*i+1] = cut[i][1];
  }
  std::sort(p, p+2*n, cmp);
  for (int i = 0; i < 2*n; ++i)
    addedge(p[i], p[(i+1)%(2*n)], true);

  for (int i = 0; i < n; ++i)
  {
    int tot = 0;
    p[tot++] = cut[i][0], p[tot++] = cut[i][1];
    for (int j = 0; j < n; ++j)
      if (i != j && intersection(cut[i][0], cut[i][1], cut[j][0], cut[j][1], tmp))
        p[tot++] = tmp;
    std::sort(p, p+tot);
    for (int i = 1; i < tot; ++i)
      addedge(p[i-1], p[i], false);
  }
  for (int i = 0; i < cnt; ++i)
  {
    O = hash[hashpos[i]];
    std::sort(v[i].begin(), v[i].end(), cmp2);
  }

  double ans = .0;
  for (int i = 0; i < cnt; ++i)
    for (vector<Segment>::iterator e = v[i].begin(); e != v[i].end(); ++e)
      if (!e->vis)
      {
        const double s = go(i, e);
        ans = std::max(ans, s);
      }

  for (int i = 0; i < cnt; ++i)
    v[i].clear();
  return ans;
}
int main()
{
  for (scanf("%d", &Testcase); Testcase--; )
  {
    scanf("%d", &n);
    printf("%.2f\n", solve());
  }
}

Comments