Lifting the Stone

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

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
 
Recommend
Eddy   |   We have carefully selected several similar problems for you:  1392 2108 2150 1348 1147 

 
  计算几何中的水题,求多边形重心。
  第一遍直接套用了模板,因为这几天过年,现在刚回家,回来时候已经很晚了也没时间仔细看具体的做法,过段时间再做一遍。
  过年了,回想这一年,最大的收获就是体会到坚持一样东西的重要性。我会继续努力,不让自己失望,也不让看着我的人失望。马年,继续加油!
 
  给大家拜个晚年!祝大家都能在新的一年里幸福,如意!马年吉祥!!
 
  上代码:
 #include <iostream>
#include <iomanip>
using namespace std;
struct point { double x, y; };
point bcenter(point pnt[], int n){
point p, s;
double tp, area = , tpx = , tpy = ;
p.x = pnt[].x; p.y = pnt[].y;
for (int i = ; i <= n; ++i) { // point: 0 ~ n-1
s.x = pnt[(i == n) ? : i].x;
s.y = pnt[(i == n) ? : i].y;
tp = (p.x * s.y - s.x * p.y); area += tp / ;
tpx += (p.x + s.x) * tp; tpy += (p.y + s.y) * tp;
p.x = s.x; p.y = s.y;
}
s.x = tpx / ( * area); s.y = tpy / ( * area);
return s;
}
point P[];
int main()
{
int T,N;
cin>>T;
cout<<setiosflags(ios::fixed)<<setprecision();
while(T--){
cin>>N;
for(int i=;i<N;i++) //从0开始输入多边形的n个点
cin>>P[i].x>>P[i].y;
point t = bcenter(P,N); //返回重心坐标
cout<<t.x<<' '<<t.y<<endl;
}
return ;
}

  重新做了一遍这个题,现在理解了求多边形重心的算法,在poj上找到了一样的题,在两个OJ上同时提交。一开始总是WA,后来看了很多题解和讨论发现是精度问题,由于除法会产生很大的误差,再小的浮点误差,累积到10w后都是很大的误差,所以应该尽量减少除法。我改进之后,算法只有加法和乘法,只有最后再/3。最后在hdu上AC,但是poj上死活AC不了,而且上面使用模板的代码放到poj上竟然超时,百思不得其解。郁闷……

  hdu上AC的代码:

 #include <stdio.h>
typedef struct {
double x,y;
}Point;
double getS(Point a,Point b,Point c) //返回三角形面积
{
return ((b.x - a.x) * (c.y - a.y) - (b.y - a.y)*(c.x - a.x))/;
}
Point getPZ(Point p[],int n) //返回多边形重心
{
Point z;
double sumx = ,sumy = ;
double sumS = ;
for(int i=;i<=n-;i++){
double S = getS(p[],p[i+],p[i]);
sumS += S;
sumx += (p[].x+p[i].x+p[i+].x)*S;
sumy += (p[].y+p[i].y+p[i+].y)*S;
}
z.x = sumx / (sumS* );
z.y = sumy / (sumS* );
return z;
}
Point p[];
int main()
{
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
Point z = getPZ(p,n);
printf("%.2lf %.2lf\n",z.x,z.y);
}
return ;
}

Freecode : www.cnblogs.com/yym2013

hdu 1115:Lifting the Stone(计算几何,求多边形重心。 过年好!)的更多相关文章

  1. hdu 1115 Lifting the Stone

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

  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. Lifting the Stone(求多边形的重心—)

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

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

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

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

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

  7. Hdoj 1115.Lifting the Stone 题解

    Problem Description There are many secret openings in the floor which are covered by a big heavy sto ...

  8. UVALive 4426 Blast the Enemy! --求多边形重心

    题意:求一个不规则简单多边形的重心. 解法:多边形的重心就是所有三角形的重心对面积的加权平均数. 关于求多边形重心的文章: 求多边形重心 用叉积搞一搞就行了. 代码: #include <ios ...

  9. Lifting the Stone 计算几何 多边形求重心

    Problem Description There are many secret openings in the floor which are covered by a big heavy sto ...

随机推荐

  1. Python 小程序,对文件操作及其它

    以下是自己写的几个对文件操作的小程序,里面涉及到文件操作,列表(集合,字典)的运用等.比方说,从文件里读取一行数据.分别存放于列表中,再对列表进行操作.如去掉里面的反复项.排序等操作. 常见对文件里行 ...

  2. java反射--获取成员变量信息

    获取成员变量信息 代码及说明: public static void printFieldMessage(Object obj) { //要获取类的信息,首先要获取类的类类型 Class c=obj. ...

  3. Spring 基于xml配置方式的AOP

    我们具体用代码来说明: 1.ArithmeticCalculator.java package com.proc; public interface ArithmeticCalculator { in ...

  4. Appium(JAVA)Windows 7系统搭建及示例运行

    Appium(JAVA)Windows 7系统搭建及示例运行 分类: Appium 2014-11-14 17:44 4323人阅读 评论(2) 收藏 举报 1.搭建Android环境 http:// ...

  5. Java入门到精通——调错篇之Eclipse No Java virtual machine was found after searching the following locations

    一.错误现象. 在一次启动Eclipse的时候弹出了以下的错误 二.错误原因 原因是没有找到javaw.exe文件的路径. 三.解决方式 在eclipse根文件夹下找到eclipse.ini增加以下一 ...

  6. PostgreSQL远端访问

    PostgreSQL默认的理念是运行在本地地址且不允许外部访问的. 如果想通过Navicat for postgreSql这种优秀的第三方软件访问需要做出如下修改: 一.启动在外部可访问的地址上 编辑 ...

  7. [转]sql:除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询

    执行sql语句: select * from ( select * from tab where ID>20 order by userID desc ) as a order by date ...

  8. 百度地图 驾车/公交查询Demo(支持多起点多终点)

    效果图: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  9. C# 获得文件名

    string strFilePaht="文件路径"; Path.GetFileNameWithoutExtension(strFilePath);这个就是获取文件名的 还有的就是用 ...

  10. Hystrix使用Commond的三种方式

    目录(?)[-] 1 依赖引入 2 使用 21 Hystrix command 211 同步执行 212 异步执行 213 反应执行 214 三种模式使用区别 22 Fallback 23 Error ...