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

Source

Central Europe 1999


思路

求多边形重心步骤如下:

  • 将原N多边形划分为N-2个三角形
  • 求每个三角形的重心和面积:重心是坐标和/3,面积用叉乘处理
  • 多边形的重心为\(G_x = \frac{\sum x_i*area_i}{3 * \sum area_i}\),\(G_y = \frac{\sum y_i*area_i}{3 * \sum area_i}\)

代码

#include<bits/stdc++.h>
using namespace std;
struct node
{
int x;
int y;
} a[1000010];
double crossMult(node a,node b)
{
return a.x*b.y-a.y*b.x;
}
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
node a,b,c;
cin >> n;
cin >> a.x >> a.y >> b.x >> b.y; n -= 2;//已经读了2个点
double area = 0.0;
double sum_xs = 0.0,sum_ys = 0.0;
while(n--)
{
cin >> c.x >> c.y;
node newb,newc;
newb.x = b.x - a.x; newb.y = b.y - a.y;
newc.x = c.x - a.x; newc.y = c.y - a.y; double tmp_area = crossMult(newb,newc)/2.0;
area += tmp_area;
sum_xs += ((a.x + b.x + c.x) * tmp_area);
sum_ys += ((a.y + b.y + c.y) * tmp_area);
b = c;
}
printf("%.2lf %.2lf\n",sum_xs/area/3.0,sum_ys/area/3.0); }
return 0;
}

Hdoj 1115.Lifting the Stone 题解的更多相关文章

  1. poj 1115 Lifting the Stone 计算多边形的中心

    Lifting the Stone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  2. hdu 1115:Lifting the Stone(计算几何,求多边形重心。 过年好!)

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  3. hdu 1115 Lifting the Stone 多边形的重心

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. hdu 1115 Lifting the Stone (数学几何)

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. hdu 1115 Lifting the Stone

    题目链接:hdu 1115 计算几何求多边形的重心,弄清算法后就是裸题了,这儿有篇博客写得很不错的: 计算几何-多边形的重心 代码如下: #include<cstdio> #include ...

  6. hdu1115 Lifting the Stone(几何,求多边形重心模板题)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1115">http://acm.hdu.edu.cn/showproblem.php ...

  7. Lifting the Stone(多边形重心)

    Lifting the Stone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  8. POJ 1385 Lifting the Stone (多边形的重心)

    Lifting the Stone 题目链接: http://acm.hust.edu.cn/vjudge/contest/130510#problem/G Description There are ...

  9. Lifting the Stone(hdoj1115)

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. 把玩Alpine linux(一):安装

    导读 Alpine Linux是一个面向安全应用的轻量级Linux发行版.它采用了musl libc和busybox以减小系统的体积和运行时资源消耗,同时还提供了自己的包管理工具apk.Alpine ...

  2. 阿里字体css代码引入方法

    1.第一步,选择自己想要的图标字体,添加入库. 2.选择下载代码. 3.我们可以发现,有如下的代码被下载下来了. 4.我们选择iconfont.css放到自己的文件夹中. 5.然后我们根据下载下来ht ...

  3. 软工网络15团队作业8——Beta阶段敏捷冲刺

    Deadline: 2018-5-31 22:00PM,以博客提交至班级博客时间为准 根据以下要求: (1)在敏捷冲刺前发布一篇博客,作为beta版敏捷冲刺的开始, (2)同时,团队在日期区间[5.2 ...

  4. centos yum install nginx

    nginx newshttp://nginx.org/ nginx news: 2017http://nginx.org/2017.html nginx: Linux packageshttps:// ...

  5. asp.net Json序列化

    Json作为一种数据传输格式与标准被广泛的使用在项目开发中,可以说简直离不开它.那么怎么来生成JSON格式的数据就成了我们首先需要解决的问题这里我们使用.net. 首先是获取数据 public ban ...

  6. 3proxy使用方法

    转自:DRL@fireinice写的教程 ******************************************************************************* ...

  7. Satis搭建composer私有库(自定义下载目录)

    在我们的日常php开发中需要使用大量的第三方包和类库, 怎么管理是一个问题, 我们用的Yii2框架, 但是并没有把composer用起来, 由于最近更换为docker部署项目, 于是想起来用compo ...

  8. node exprss-session 和connect-mongo

    let express = require('express'); let session = require('express-session'); let app = new express(); ...

  9. switch变种玩法

    标准版本: switch(表达式) { case 值1: 语句体1; break; case 值2: 语句体2; break; ... default: 语句体n+; break; } switch: ...

  10. [转帖]Htop 使用详解

    htop使用详解 https://www.cnblogs.com/yqsun/p/5396363.html 一.Htop的使用简介 大家可能对top监控软件比较熟悉,今天我为大家介绍另外一个监控软件H ...