hdu-1115 Lifting the Stone(求多边形的重心)

来源:互联网 发布:ocr文字识别算法原理 编辑:程序博客网 时间:2024/05/16 04:49
题目链接:点击打开链接

Lifting the Stone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6975    Accepted Submission(s): 2923


Problem Description
There are many secret openings in the floor which are covered by a big heavy stone. When the stone is lifted up, a special mechanism detects this and activates poisoned arrows that are shot near the opening. The only possibility is to lift the stone very slowly and carefully. The ACM team must connect a rope to the stone and then lift it using a pulley. Moreover, the stone must be lifted all at once; no side can rise before another. So it is very important to find the centre of gravity and connect the rope exactly to that point. The stone has a polygonal shape and its height is the same throughout the whole polygonal area. Your task is to find the centre of gravity for the given polygon. 
 

Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer N (3 <= N <= 1000000) indicating the number of points that form the polygon. This is followed by N lines, each containing two integers Xi and Yi (|Xi|, |Yi| <= 20000). These numbers are the coordinates of the i-th point. When we connect the points in the given order, we get a polygon. You may assume that the edges never touch each other (except the neighboring ones) and that they never cross. The area of the polygon is never zero, i.e. it cannot collapse into a single line. 
 

Output
Print exactly one line for each test case. The line should contain exactly two numbers separated by one space. These numbers are the coordinates of the centre of gravity. Round the coordinates to the nearest number with exactly two digits after the decimal point (0.005 rounds up to 0.01). Note that the centre of gravity may be outside the polygon, if its shape is not convex. If there is such a case in the input data, print the centre anyway. 
 

Sample Input
245 00 5-5 00 -541 111 111 111 11
 

Sample Output
0.00 0.006.00 6.00
题意:给出n个点,求出构成的多边形的重心
思路:已知三角形的重心是((x1+x2+x3)/3,(y1+y2+y3)/3) 我们可不可以直接拓展到任意多边形呢?
((x1+x2+...xn)/n,(y1+y2+...yn)/n) 答案是对于一般的多边形(质量分布在顶点)可以。而对于质量均匀且分布在面积上的则不可以。
所以对于求多边形的重心我们的通用求法是:
C=sigma(Ai * Ci) / A     (i=1…N)//Ai代表了每一个三角形的面积,A为总面积
Ci=Centroid(△ O PiPi+1)
   = (O + ↑Pi+↑Pi+1 )/3 //每个三角形的重心
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define N 1000005struct Node{    double x,y;} node[N];int main(){    int T,n;    double S,s;    scanf("%d",&T);    while(T--)    {        S=0.0,s;        Node ans;        ans.x=0.0,ans.y=0.0;        scanf("%d",&n);        for(int i=1; i<=n; i++)            scanf("%lf %lf",&node[i].x,&node[i].y);        for(int i=2; i<n; i++)        {            s=((node[i].x-node[1].x)*(node[i+1].y-node[1].y)-(node[i+1].x-node[1].x)*(node[i].y-node[1].y))/2.0;            S+=s;            ans.x+=s*(node[i].x+node[i+1].x+node[1].x);            ans.y+=s*(node[i].y+node[i+1].y+node[1].y);        }        printf("%.2lf %.2lf\n",ans.x/(3*S),ans.y/(3*S));    }    return 0;}

或者:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define N 1000005struct Node{    double x,y;} node[N];int main(){    int T,n;    double S,s;    scanf("%d",&T);    while(T--)    {        S=0.0,s;        Node ans;        ans.x=0.0,ans.y=0.0;        scanf("%d",&n);        for(int i=1; i<=n; i++)            scanf("%lf %lf",&node[i].x,&node[i].y);        for(int i=1; i<n; i++)        {            s=(node[i].x*node[i+1].y-node[i+1].x*node[i].y)/2.0;            S+=s;            ans.x+=s*(node[i].x+node[i+1].x);            ans.y+=s*(node[i].y+node[i+1].y);        }        s=(node[n].x*node[1].y-node[1].x*node[n].y)/2.0;        S+=s;        ans.x+=s*(node[n].x+node[1].x);        ans.y+=s*(node[n].y+node[1].y);        printf("%.2lf %.2lf\n",ans.x/(3*S),ans.y/(3*S));    }    return 0;}




0 0