HDU 3685 polygon Center of Gravity + convex hull

Source: Internet
Author: User
Rotational painting

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2498 accepted submission (s): 702


Problem descriptionjosh 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 astream mical 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 your 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.
 

 

Inputthe 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.

 

Outputfor 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.

 

Sample input240 0100 099 11 160 00 101 101 110 0

 

Sample output23 HintThe sample test cases can be demonstrated by Figure 1 and figure 2 in description part. It is a stable number to place a polygon on the plane (the center of gravity is within the support point, and the support point is unstable.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <vector> 6 # include <algorithm> 7 using namespace std; 8 9 const double EPS = 1e-8; 10 const double Pi = ACOs (-1.0); 11 struct point 12 {13 Double X, Y; 14 point (double x = 0, double Y = 0): x (x), y (y) {}15}; 16 typedef point vector; 17 vector operator + (vector A, vector B) {return vector (. X + B. x,. Y + B. y);} 18 Vector Operator-(vector A, vector B) {return vector (. x-B.x,. y-B.y);} 19 vector operator * (vector A, Double P) {return vector (. x * P,. y * P);} 20 vector operator/(vector A, Double P) {return vector (. x/P,. y/P);} 21 bool operator <(const point & A, const point & B) 22 {23 return. x <B. X | (. X = B. X &. Y <B. y); 24} 25 int DCMP (Double X) 26 {27 if (FABS (x) <EPS) return 0; 28 else return x <0? -1:1; 29} 30 bool operator = (const point & A, const point & B) {31 return (DCMP (. x-b.x) = 0 & DCMP (. y-b.y) = 0); 32} 33 double dot (vector A, vector B) {return. x * B. X +. y * B. y;} // dot product 34 double cross (vector A, vector B) {return. x * B. y-A.y * B. x;} // Cross Product 35 point getlineprojection (point P, point A, point B) // P projection point on a straight line AB 36 {37 vector v = B-A; 38 return a + V * (dot (v, P-A)/DOT (V, V); 39} 40 bool onsegment (point P, Point A1, point A2) // whether the point is on a straight line (excluding the endpoint) 41 {42 return DCMP (Cross (a1-p, a2-p) = 0 & DCMP (dot (a1-p, a2-p )) <0; 43} 44 point getcenter (vector <point> P) // the center of gravity of the polygon 45 {46 Double area = 0; 47 point c = point ); 48 int I, n = P. size (); 49 for (I = 1; I <n-1; I ++) 50 {51 double temp = cross (P [I]-P [0], P [I + 1]-P [0]); 52 c. X + = temp * (p [I]. X + P [I + 1]. X + P [0]. x)/3.0; 53 c. Y + = temp * (p [I]. Y + P [I + 1]. Y + P [0]. y)/3.0; 54 area + = temp; 55} 56 c. x/= area; C. y/= area; 57 Return C; 58} 59 vector <point> convexhull (vector <point> & P) // returns the convex hull 60 {61 sort (P. begin (), P. end (); 62 p. erase (unique (P. begin (), P. end (), p. end (); 63 int I, n = P. size (); 64 int m = 0; 65 vector <point> CH (n + 1); 66 for (I = 0; I <n; I ++) {67 while (M> 1 & cross (CH M-1]-ch [m-2], p [I]-ch [m-2]) <= 0) m --; 68 ch [M ++] = P [I]; 69} 70 int K = m; 71 for (I = n-2; I> = 0; I --) {72 while (M> K & cross (CH M-1]-ch [m-2], p [I]-ch [m-2]) <= 0) m --; 73 ch [M ++] = P [I]; 74} 75 if (n> 1) m --; 76 ch. resize (m); 77 return ch; 78} 79 80 void solve (vector <point> P, Point Center) 81 {82 int ans = 0, n = P. size (); 83 for (INT I = 0; I <n; I ++) 84 {85 Point T = getlineprojection (center, P [I], P [(I + 1) % N]); 86 If (onsegment (T, P [I], p [(I + 1) % N]) ans ++; 87} 88 printf ("% d \ n", ANS); 89} 90 91 int main () 92 {93 int I, t, n; 94 Double X, Y; 95 vector <point> P; 96 scanf ("% d", & T); 97 while (t --) 98 {99 p. clear (); 100 scanf ("% d", & N); 101 for (I = 0; I <n; I ++) 102 {103 scanf ("% lf", & X, & Y); p. push_back (point (x, y); 104} 105 point center = getcenter (p); 106 P = convexhull (p); 107 solve (p, center ); 108} 109 return 0; 110}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.