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

  1. 2
  2. 4
  3. 5 0
  4. 0 5
  5. -5 0
  6. 0 -5
  7. 4
  8. 1 1
  9. 11 1
  10. 11 11
  11. 1 11

Sample Output

  1. 0.00 0.00
  2. 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}\)

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct node
  4. {
  5. int x;
  6. int y;
  7. } a[1000010];
  8. double crossMult(node a,node b)
  9. {
  10. return a.x*b.y-a.y*b.x;
  11. }
  12. int main()
  13. {
  14. int t;
  15. cin >> t;
  16. while(t--)
  17. {
  18. int n;
  19. node a,b,c;
  20. cin >> n;
  21. cin >> a.x >> a.y >> b.x >> b.y;
  22. n -= 2;//已经读了2个点
  23. double area = 0.0;
  24. double sum_xs = 0.0,sum_ys = 0.0;
  25. while(n--)
  26. {
  27. cin >> c.x >> c.y;
  28. node newb,newc;
  29. newb.x = b.x - a.x; newb.y = b.y - a.y;
  30. newc.x = c.x - a.x; newc.y = c.y - a.y;
  31. double tmp_area = crossMult(newb,newc)/2.0;
  32. area += tmp_area;
  33. sum_xs += ((a.x + b.x + c.x) * tmp_area);
  34. sum_ys += ((a.y + b.y + c.y) * tmp_area);
  35. b = c;
  36. }
  37. printf("%.2lf %.2lf\n",sum_xs/area/3.0,sum_ys/area/3.0);
  38. }
  39. return 0;
  40. }

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. NGINX Docs | Load Balancing Apache Tomcat Servers with NGINX Open Source and NGINX Plus

    NGINX Docs | Load Balancing Apache Tomcat Servers with NGINX Open Source and NGINX Plushttps://docs. ...

  2. Mysql之常用操作(2)

    Windows服务 -- 启动MySQL net start mysql -- 创建Windows服务 sc create mysql binPath= mysqld_bin_path(注意:等号与值 ...

  3. JMeter学习FTP测试计划(转)

    FTP服务主要提供上传和下载功能.有时间需要我们测试服务器上传和下载的性能.在这里我通过JMeter做一个FTP测试计划的例子. 1.创建一个线程组 2.线程组--->添加--->配置元件 ...

  4. C#设计模式之5:简单工厂和工厂方法模式

    工厂模式包含三种,简单工厂模式,工厂方法模式,抽象工厂模式.这三种都是解决了一个问题,那就是对象的创建问题.他们的职责就是将对象的创建和对象的使用分离开来. 当我们创建对象的时候,总是会new一个对象 ...

  5. 面象对象设计原则之七:合成复用原则(Composition/Aggregate Reuse Principle, CARP)

    合成复用原则又称为组合/聚合复用原则(Composition/Aggregate Reuse Principle, CARP),其定义如下: 合成复用原则(Composite Reuse Princi ...

  6. ssh登录

    ssh 用户名@IP地址 -p 端口号 ssh root@127.0.0.1 -p 2222

  7. Day 5-3 多态与多态性

    多态与多态性 鸭子类型 多态与多态性 多态:一类事物有多种形态.比如,动物有多种形态,人,狗,猪,豹子.水也有多种形态,冰,雪,水蒸气. #多态:同一类事物的多种形态 import abc class ...

  8. Spring Boot 框架学习 (一)配置并运行Spring Boot 框架

    下载开发工具: 下载完成打开以后,第一步检查环境 查看jdk是否配置: 接着一定要注意,maven通常情况下它是没有给你配置的,要自行配置: 右键新建: 然后依赖选择web.跟Mybatis就行了. ...

  9. 在Hmtl页面中只让其中单独的一个div隐藏滚动条但是仍可滚动浏览下边的内容

    <style> .box ::-webkit-scrollbar {width: 0px;} </style> <div class="box"> ...

  10. LodopFuncs.js和CLodopFuncs.js区别和联系

    所在位置:LodopFuncs.js可以在官网下载中心综合版里下载到.CLodopfuncs.js在C-Lodop服务缓存中,C-Lodop启动的时候才能访问到. 需不需要下载放置到项目里:(客户端本 ...