hdu 3685 多边形重心+凸包

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

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.

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.

Sample Input

2
4
0 0
100 0
99 1
1 1
6
0 0
0 10
1 10
1 1
10 1
10 0

Sample Output

2
3

Hint

The sample test cases can be demonstrated by Figure 1 and Figure 2 in Description part.

题目大意:给一个多边形,问把它放到平面上是稳定状态的(重心在支撑点以内,在支撑点是不稳定的)种数。

  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(A.x+B.x,A.y+B.y);}
 18 Vector operator -(Vector A,Vector B){return Vector(A.x-B.x,A.y-B.y);}
 19 Vector operator *(Vector A,double p){return Vector(A.x*p,A.y*p);}
 20 Vector operator /(Vector A,double p){return Vector(A.x/p,A.y/p);}
 21 bool operator < (const Point &a,const Point &b)
 22 {
 23     return a.x<b.x||(a.x==b.x&&a.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(a.x-b.x)==0 && dcmp(a.y-b.y)==0);
 32 }
 33 double Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;}//点积
 34 double Cross(Vector A,Vector B){ return A.x*B.y-A.y*B.x;}//叉积
 35 Point GetLineProjection(Point P,Point A,Point B)//P在直线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)//点是否在直线上(不包括端点)
 41 {
 42     return dcmp(Cross(a1-p,a2-p))==0 && dcmp(Dot(a1-p,a2-p))<0;
 43 }
 44 Point getcenter(vector<Point> p)//多边形的重心
 45 {
 46     double area=0;
 47     Point c=Point(0,0);
 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)//求凸包
 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%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 }

hdu 3685 多边形重心+凸包

时间: 2024-10-03 09:08:28

hdu 3685 多边形重心+凸包的相关文章

Hdu 3685 Rotational Painting(多边形重心+凸包)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3685 思路:先求出多边形重心,放置的边一定为凸包边.判断重心是否落在边之间(求点到直线与点到线段的距离,判断). 4 0 0 4 0 8 4 4 4 注意这种情况,重心不能在凸包边端点的垂线上. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using name

hdu 1115(多边形重心问题)

Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6971    Accepted Submission(s): 2919 Problem Description There are many secret openings in the floor which are covered by a big

hdu 3685 10 杭州 现场 F - Rotational Painting 重心

F - Rotational Painting Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3685 Appoint description:  System Crawler  (2014-11-09) Description Josh Lyman is a gifted painter. One of his great works

hdu3685 Rotational Painting 求多边形重心和凸包

http://acm.hdu.edu.cn/showproblem.php?pid=3685 Rotational Painting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2614    Accepted Submission(s): 737 Problem Description Josh Lyman is a gifted

hdu 1115(计算多边形重心)

题意:已知一多边形没有边相交,质量分布均匀.顺序给出多边形的顶点坐标,求其重心. 分析: 求多边形重心的题目大致有这么几种: 1,质量集中在顶点上.n个顶点坐标为(xi,yi),质量为mi,则重心 X = ∑( xi×mi ) / ∑mi Y = ∑( yi×mi ) / ∑mi 特殊地,若每个点的质量相同,则 X = ∑xi / n Y = ∑yi / n 2,质量分布均匀.这个题就是这一类型,算法和上面的不同. 特殊地,质量均匀的三角形重心: X = ( x0 + x1 + x2 ) / 3

HDU 1115(求质量均匀分布的多边形重心 物理)

题意是给一个 n 边形,给出沿逆时针方向分布的各顶点的坐标,求出 n 边形的重心. 求多边形重心的情况大致上有三种: 一.多边形的质量都分布在各顶点上,像是用轻杆连接成的多边形框,各顶点的坐标为Xi,Yi,质量为mi,则重心坐标为: X = ∑( xi * mi ) /  ∑ mi ; Y = ∑( yi * mi)  / ∑ mi; 若每个顶点的质量相等,则重心坐标为: X = ∑ xi / n; Y = ∑ yi / n; 二.多边形的质量分布均匀,像是用密度相同的材料制成的多边形板子,多采

hdu1115(多边形重心算法)

题目意思: 给出一个n边形的n个顶点,求出这个n边形的重心坐标. http://acm.hdu.edu.cn/showproblem.php?pid=1115 题目分析: /** *出处:http://blog.csdn.net/ysc504/article/details/8812339 *①质量集中在顶点上 *  n个顶点坐标为(xi,yi),质量为mi,则重心 * X = ∑( xi×mi ) / ∑mi * Y = ∑( yi×mi ) / ∑mi * 特殊地,若每个点的质量相同,则 *

多边形重心模板

HDU 1115 Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5719    Accepted Submission(s): 2391 Problem Description There are many secret openings in the floor which are covered

UVALive 4426 Blast the Enemy! --求多边形重心

题意:求一个不规则简单多边形的重心. 解法:多边形的重心就是所有三角形的重心对面积的加权平均数. 关于求多边形重心的文章: 求多边形重心 用叉积搞一搞就行了. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #define Mod 100000000