[POJ 1385] Lifting the Stone (计算几何)

题目链接:http://poj.org/problem?id=1385

题目大意:给你一个多边形的点,求重心。

首先,三角形的重心: ( (x1+x2+x3)/3 , (y1+y2+y3)/3 )

然后多边形的重心就是将多边形划分成很多个三角形,以三角形面积为权值,将每个三角形的重心加权平均。

注意:

pair<double,double>会MLE。。

fabs会损失精度?(这个我也不知道),因此在用向量叉积求三角形面积的时候最好是直接让面积求出来就是正的。。否则fabs就WA了。。。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cmath>
 5 using namespace std;
 6 typedef long long LL;
 7 #define EPS 1e-8
 8 #define X first
 9 #define Y second
10
11 typedef struct _POINT{ // 用pair<double,double>会MLE!!
12     double first;
13     double second;
14 }POINT;
15
16 typedef POINT VECTOR;
17 double operator*(VECTOR x,VECTOR y){
18     double res;
19     res = x.X*y.Y-x.Y*y.X ;
20     return res;
21 }
22 VECTOR operator-(POINT x,POINT y){
23     VECTOR res;
24     res.X = x.X - y.X ;
25     res.Y = x.Y - y.Y ;
26     return res;
27 }
28
29 const int MAX_N = 1000001;
30 int T,N;
31 POINT pt[MAX_N];
32
33 POINT getCenter(POINT points[],int n){
34     POINT res;
35     double area,areamulx,areamuly;
36     area = 0.0; areamulx = 0.0 ; areamuly = 0.0;
37     for(int i=1;i<n-1;i++){
38         VECTOR v1 = points[i]-points[0];
39         VECTOR v2 = points[i+1]-points[i]; // 后面如果fabs会损失精度。。
40         double tarea = v1 * v2 / 2.0;
41         area += tarea;
42         areamulx += (points[0].X+points[i].X+points[i+1].X)*tarea;
43         areamuly += (points[0].Y+points[i].Y+points[i+1].Y)*tarea;
44     }
45     res.X = areamulx / (3.0*area);
46     res.Y = areamuly / (3.0*area);
47     return res;
48 }
49
50 int main(){
51     scanf("%d",&T);
52     while( T-- ){
53         scanf("%d",&N);
54         for(int i=0;i<N;i++){
55             scanf("%lf%lf",&pt[i].X,&pt[i].Y);
56         }
57         POINT ans = getCenter(pt,N);
58         printf("%.2lf %.2lf\n",ans.X+EPS,ans.Y+EPS);
59     }
60     return 0;
61 }
时间: 2024-10-10 05:37:23

[POJ 1385] Lifting the Stone (计算几何)的相关文章

HDU 1115 Lifting the Stone

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

ACM-计算几何之Lifting the Stone——hdu1115

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

Lifting the Stone(hdoj1115)

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

Lifting the Stone(求任意多边形的重心)

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

hdu 1115 Lifting the Stone (数学几何)

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

HDOJ 1115 Lifting the Stone 多边形重心

来自:http://blog.csdn.net/tiaotiaoyly/article/details/2087498 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 step 7.1.3)Lifting the Stone(求凸多边形的重心)

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

Lifting the Stone(hdu1115)多边形的重心

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

Lifting the Stone

我们需要把一块石头平稳的从地板上拿起来.石头的底面是多边形且各个部分的高度都一样,我们需要找出石头的重心. input 测试案例  T; 每组第一行给出N,表示定点数. 接下来N行,每行连个数,表示坐标.定点按顺时针或逆时针给出. output 重心坐标.两个数中间一个空格,每个数保留两位小数. 思路 把多变形分成N-2个三角形,求出重心,重心的质量.(其实是质心) 根据   x=Σ(xi*mi)/Σmi,有因为mi 正比于体积 正比于 面积. 所以利用叉乘求面积. 1 #include"ios