hdu1115 Lifting the Stone(计算几何_多边形重心)

Lifting the Stone

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


 

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
 
 

参考博客:https://blog.csdn.net/lttree/article/details/24720007

 

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

   ①质量集中在顶点上。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

  

本题求质量分布均匀的多边形的重心,若将其转化为质量集中在顶点上的多边形,就可以套用第一种情况求重心了。所以把原多边形的n个顶点与坐标原点相连划分成n个三角形,这n个三角形是质量分布均匀的,其重心可以由第二种情况求出。将n个三角形的重心为顶点构成一个新的多边形,那么这个新的多边形的质量集中于顶点上,可以套用第一种情况求解,求出的新多边形重心即原多边形重心。

具体见注释:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int N = 1e6 + 20;

struct Point
{
    double x, y;
    ///叉积
    double operator ^ (const Point &b)const
    {
        return x * b.y - y * b.x;
    }
}p[N];

///质量均匀多边形重心
Point bcenter(Point p[], int n)
{
    Point res;
    double sum = 0;                                 ///多边形面积
    double xx = 0, yy = 0;                          ///多边形重心坐标
    for(int i = 0; i < n; ++i)                      ///将多边形划分成n个三角形
    {
        double tmp = (p[i] ^ p[(i + 1) % n]);       ///三角形的面积的2倍
        sum += tmp / 2;                             ///多边形的面积

        ///三角形的重心与质量加权(由于质量分布均匀,质量与面积成正比,故相当于面积)
        ///三角形的重心与质量(面积)相乘(重心横坐标实际为 X = (0 + p[1].x + p[2].x) / 3  三角形面积实际为tmp / 2  为了防止精度受损,暂且不进行除法)
        xx += (p[i].x + p[(i + 1) % n].x) * tmp;
        yy += (p[i].y + p[(i + 1) % n].y) * tmp;
    }
    ///tmp / 2   重心坐标 / 3  故 / 6
    res.x = xx / (6 * sum);
    res.y = yy / (6 * sum);
    return res;
}

int main()
{
    int n, t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        for(int i = 0; i < n; ++i)
        {
            scanf("%lf%lf", &p[i].x, &p[i].y);
        }
        Point ans = bcenter(p, n);
        printf("%.2f %.2f\n", ans.x, ans.y);
    }
    return 0;
}
全部评论

相关推荐

02-16 01:39
南昌大学 Java
重剑Ds:感觉不太可能 后端都减飞了 根本不缺人
点赞 评论 收藏
分享
03-06 18:20
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
6678次浏览 64人参与
# 你的实习产出是真实的还是包装的? #
1331次浏览 32人参与
# 米连集团26产品管培生项目 #
4804次浏览 206人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7120次浏览 37人参与
# 简历第一个项目做什么 #
31334次浏览 315人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186557次浏览 1115人参与
# 巨人网络春招 #
11223次浏览 223人参与
# 研究所笔面经互助 #
118794次浏览 577人参与
# 面试紧张时你会有什么表现? #
30416次浏览 188人参与
# 简历中的项目经历要怎么写? #
309597次浏览 4167人参与
# 职能管理面试记录 #
10726次浏览 59人参与
# AI时代,哪些岗位最容易被淘汰 #
62775次浏览 750人参与
# 网易游戏笔试 #
6382次浏览 83人参与
# 把自己当AI,现在最消耗你token的问题是什么? #
7014次浏览 154人参与
# 腾讯音乐求职进展汇总 #
160447次浏览 1107人参与
# 从哪些方向判断这个offer值不值得去? #
56712次浏览 357人参与
# 正在春招的你,也参与了去年秋招吗? #
362761次浏览 2632人参与
# 你怎么看待AI面试 #
179447次浏览 1184人参与
# 小红书求职进展汇总 #
226916次浏览 1357人参与
# 你的房租占工资的比例是多少? #
92153次浏览 896人参与
# 校招笔试 #
467856次浏览 2955人参与
# 经纬恒润求职进展汇总 #
155724次浏览 1085人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务