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

2 4 5 0 0 5 -5 0 0 -5 4 1 1 11 1 11 11 1 11

Sample Output

0.00 0.00 6.00 6.00

这道题是求多边形重心的题目,可以做个模板。

该题目中的点顺序是按逆时针顺序给的。

求多边形重心分两种情况:

①质量集中在顶点上。n个顶点坐标为(xi,yi),质量为mi,则重心

X = ∑( xi×mi ) / ∑mi        Y = ∑( yi×mi ) / ∑mi

特殊地,若每个点的质量相同,则    X = ∑xi / n        Y = ∑yi / n

②质量分布均匀。这个题就是这一类型,算法和上面的不同。

特殊地,质量均匀的三角形重心:

X = ( x0 + x1 + x2 ) / 3        Y = ( y0 + y1 + y2 ) / 3

题解参考自:http://blog.csdn.net/lttree/article/details/24720007

所以,这道题解法:

1.以一个点为顶点,做多个三角形,然后求每个三角形的重心与质量。

2.然后用每个三角形的重心为顶点再构成一个多边形。

3.这个多边形就属于上述的第一种情况了,质量在顶点上的多边形面积。

4.套公式就OK拉~

PS:注意几点:

由于三角形质量与面积成正比(WHY?因为质量分布均匀~。~),所以质量可用面积来代替。

再者用叉积求三角形面积要算正负情况(正负号要保留),

此举是为了防止多边形为凹多边形的情况,三角形面积会在多边形外。

OK~翠花,上模板~,此模板也会更新在我整理的     计算几何模板          中哟~~~

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#define x first
#define y second
using namespace std;

typedef long long LL;
typedef pair<double,double> Point;
const int N = 10010;
int n;
vector<Point>P;

Point cal()
{
    Point p ,s ;
    double tp , area = 0 , tpx = 0 ,tpy = 0;
    p.x = P[0].x ,p.y = P[0].y;
    for( int i = 1 ; i <= n  ;++i )
    {
        if(i == n ) s.x = P[0].x , s.y = P[0].y;
        else s.x = P[i].x , s.y = P[i].y;
        tp = ( p.x * s.y - p.y *s.x );
        area += tp / 2.0 ;
        tpx += ( p.x + s.x )* tp ;
        tpy += ( p.y + s.y )* tp ;
        p.x =s.x ; p.y = s.y;
    }
    s.x = tpx / (6*area);
    s.y = tpy / (6*area);
    return s;
}
void run()
{
    double xx,yy;
    P.clear();
    scanf("%d",&n);
    for(int i = 0 ; i< n ;++i){
        scanf("%lf%lf",&xx,&yy);
        P.push_back(Point(xx,yy));
    }
    Point res = cal();
    printf("%.2lf %.2lf\n",res.x,res.y);
}
int main()
{
    #ifdef LOCAL
//        cout<<"1"<<endl;
        freopen("in.txt","r",stdin);
    #endif // LOCAL
    int cas =1 ,_;
    cin>>_;
    while(_--)
    {
        run();
    }
    return 0;
}
时间: 2024-10-05 16:08:33

HDU 1115 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): 5203    Accepted Submission(s): 2155 Problem Description There are many secret openings in the floor which are covered by a big

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

题目链接:传送门 分析: 求多边形的重心的方法:传送门 代码如下: #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> using namespace std; const double eps = 1e-10; struct Point{ double x,y; Point():x(0),y(0){} Poi

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

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 5996 dingyeye loves stone(博弈)

题目链接:hdu 5996 dingyeye loves stone 题意: 给你一棵树,树的每一个节点有a[i]个石子,每个人可以将这个节点的石子移向它的父亲,如果没有合法操作,那么就算输,现在给你当前的局面,问你能否赢 题解: 设根节点的深度为0,将所有深度为奇数的节点的石子数目xor起来,则先手必胜当且仅当这个xor和不为0. 证明同阶梯博弈.对于偶深度的点上的石子,若对手移动它们,则可模仿操作:对于奇深度上的石子,移动一次即进入偶深度的点. 时空复杂度O(n). 1 #include<b

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