Problem Description
Josh Lyman is a gifted painter. One of his great works is a glass painting. He creates some well-designed lines on one side of a thick and polygonal glass, and renders it by some special dyes. The most fantastic thing is that it can generate different meaningful paintings by rotating the glass. This method of design is called “Rotational Painting (RP)” which is created by Josh himself.
You are a fan of Josh and you bought this glass at the astronomical sum of money. Since the glass is thick enough to put erectly on the table, you want to know in total how many ways you can put it so that you can enjoy as many as possible different paintings hiding on the glass. We assume that material of the glass is uniformly distributed. If you can put it erectly and stably in any ways on the table, you can enjoy it.
More specifically, if the polygonal glass is like the polygon in Figure 1, you have just two ways to put it on the table, since all the other ways are not stable. However, the glass like the polygon in Figure 2 has three ways to be appreciated.
Pay attention to the cases in Figure 3. We consider that those glasses are not stable.
You are a fan of Josh and you bought this glass at the astronomical sum of money. Since the glass is thick enough to put erectly on the table, you want to know in total how many ways you can put it so that you can enjoy as many as possible different paintings hiding on the glass. We assume that material of the glass is uniformly distributed. If you can put it erectly and stably in any ways on the table, you can enjoy it.
More specifically, if the polygonal glass is like the polygon in Figure 1, you have just two ways to put it on the table, since all the other ways are not stable. However, the glass like the polygon in Figure 2 has three ways to be appreciated.
Pay attention to the cases in Figure 3. We consider that those glasses are not stable.
Input
The input file contains several test cases. The first line of the file contains an integer T representing the number of test cases.
For each test case, the first line is an integer n representing the number of lines of the polygon. (3<=n<=50000). Then n lines follow. The ith line contains two real number xi and yi representing a point of the polygon. (xi, yi) to (xi+1, yi+1) represents a edge of the polygon (1<=i<n), and (xn,yn) to (x1, y1) also represents a edge of the polygon. The input data insures that the polygon is not self-crossed.
For each test case, the first line is an integer n representing the number of lines of the polygon. (3<=n<=50000). Then n lines follow. The ith line contains two real number xi and yi representing a point of the polygon. (xi, yi) to (xi+1, yi+1) represents a edge of the polygon (1<=i<n), and (xn,yn) to (x1, y1) also represents a edge of the polygon. The input data insures that the polygon is not self-crossed.
Output
For each test case, output a single integer number in a line representing the number of ways to put the polygonal glass stably on the table.
题目大意:给一个简单多边形,问有多少种方法可以把这个多边形竖直稳定地放在一个平面上,如图所示。
思路:物理学告诉我们,要解决这个问题,首先要求出这个多边形质心,然后枚举每一种放法,看质心到地板的垂线是否在底边之间。
令G(i) = cross(p[i], p[i+1]), 质心为t,多边形的点集为p,cross为叉积。
那么t.x = sum(cross(p[i], p[i+1]) / 2 * (p[i].x + p[i + 1].x) / 3) / sum(cross(p[i], p[i+1]) / 2)
t.y = sum(cross(p[i], p[i+1]) / 2 * (p[i].y + p[i + 1].y) / 3) / sum(cross(p[i], p[i+1]) / 2)
这里不证明。
然后在不考虑稳定的情况下,多边形的放法显然取决于这个多边形的凸包的边数。
枚举凸包的每一条边,判断质心到地板的垂线的垂足是否在凸包的那条边之中。
若质心为O,底边的两点分别为A、B,那么垂足在AB上当且仅当∠OAB和∠OBA都为锐角。
那么向量AO和向量AB的点积为正数,那么∠OAB为锐角,∠OBA同理。
此题解决。
代码(281MS):
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstring> 5 #include <vector> 6 using namespace std; 7 typedef long long LL; 8 9 const int MAXN = 50010; 10 const double EPS = 1e-10; 11 12 inline int sgn(double x) { 13 return (x > EPS) - (x < -EPS); 14 } 15 16 struct Point { 17 double x, y; 18 Point(double x = 0, double y = 0): x(x), y(y) {} 19 void read() { 20 scanf("%lf%lf", &x, &y); 21 } 22 Point operator + (const Point &rhs) const { 23 return Point(x + rhs.x, y + rhs.y); 24 } 25 Point operator - (const Point &rhs) const { 26 return Point(x - rhs.x, y - rhs.y); 27 } 28 double operator * (const Point &rhs) const { 29 return x * rhs.x + y * rhs.y; 30 } 31 Point operator / (const double &rhs) const { 32 return Point(x / rhs, y / rhs); 33 } 34 bool operator < (const Point &rhs) const { 35 if(y != rhs.y) return y < rhs.y; 36 return x < rhs.x; 37 } 38 }; 39 typedef Point Vector; 40 41 double cross(const Point &a, const Point &b) { 42 return a.x * b.y - a.y * b.x; 43 } 44 45 double cross(const Point &sp, const Point &op, const Point &ep) { 46 return cross(sp - op, ep - op); 47 } 48 49 void Graham_scan(Point *p, int n, int *stk, int &top) { 50 sort(p, p + n); 51 top = 1; 52 stk[0] = 0; stk[1] = 1; 53 for(int i = 2; i < n; ++i) { 54 while(top && cross(p[stk[top - 1]], p[stk[top]], p[i]) <= 0) --top; 55 stk[++top] = i; 56 } 57 int len = top; 58 stk[++top] = n - 2; 59 for(int i = n - 3; i >= 0; --i) { 60 while(top != len && cross(p[stk[top - 1]], p[stk[top]], p[i]) <= 0) --top; 61 stk[++top] = i; 62 } 63 } 64 65 Point barycenter(Point *p, int n) { 66 double area = 0; 67 Point res; 68 for(int i = 0; i < n; ++i) { 69 double t = cross(p[i], p[i + 1]) / 2; 70 res.x += t * (p[i].x + p[i + 1].x) / 3; 71 res.y += t * (p[i].y + p[i + 1].y) / 3; 72 area += t; 73 } 74 return res / area; 75 } 76 77 Point p[MAXN]; 78 int stk[MAXN], top; 79 int n, T; 80 81 int main() { 82 scanf("%d", &T); 83 while(T--) { 84 scanf("%d", &n); 85 for(int i = 0; i < n; ++i) p[i].read(); 86 p[n] = p[0]; 87 Point O = barycenter(p, n); 88 Graham_scan(p, n, stk, top); 89 90 int ans = 0; 91 for(int i = 0; i < top; ++i) { 92 Point &A = p[stk[i]], &B = p[stk[i + 1]]; 93 ans += (sgn((O - A) * (B - A)) > 0 && sgn((O - B) * (A - B)) > 0); 94 } 95 printf("%d ", ans); 96 } 97 }