题目链接:http://poj.org/problem?id=1385

题目大意:给你一个多边形的点,求重心。

首先,三角形的重心: ( (x1+x2+x3)/3 , (y1+y2+y3)/3 )

然后多边形的重心就是将多边形划分成很多个三角形,以三角形面积为权值,将每个三角形的重心加权平均。

注意:

pair<double,double>会MLE。。

fabs会损失精度?(这个我也不知道),因此在用向量叉积求三角形面积的时候最好是直接让面积求出来就是正的。。否则fabs就WA了。。。

代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <cmath>
  5. using namespace std;
  6. typedef long long LL;
  7. #define EPS 1e-8
  8. #define X first
  9. #define Y second
  10.  
  11. typedef struct _POINT{ // 用pair<double,double>会MLE!!
  12. double first;
  13. double second;
  14. }POINT;
  15.  
  16. typedef POINT VECTOR;
  17. double operator*(VECTOR x,VECTOR y){
  18. double res;
  19. res = x.X*y.Y-x.Y*y.X ;
  20. return res;
  21. }
  22. VECTOR operator-(POINT x,POINT y){
  23. VECTOR res;
  24. res.X = x.X - y.X ;
  25. res.Y = x.Y - y.Y ;
  26. return res;
  27. }
  28.  
  29. const int MAX_N = ;
  30. int T,N;
  31. POINT pt[MAX_N];
  32.  
  33. POINT getCenter(POINT points[],int n){
  34. POINT res;
  35. double area,areamulx,areamuly;
  36. area = 0.0; areamulx = 0.0 ; areamuly = 0.0;
  37. for(int i=;i<n-;i++){
  38. VECTOR v1 = points[i]-points[];
  39. VECTOR v2 = points[i+]-points[i]; // 后面如果fabs会损失精度。。
  40. double tarea = v1 * v2 / 2.0;
  41. area += tarea;
  42. areamulx += (points[].X+points[i].X+points[i+].X)*tarea;
  43. areamuly += (points[].Y+points[i].Y+points[i+].Y)*tarea;
  44. }
  45. res.X = areamulx / (3.0*area);
  46. res.Y = areamuly / (3.0*area);
  47. return res;
  48. }
  49.  
  50. int main(){
  51. scanf("%d",&T);
  52. while( T-- ){
  53. scanf("%d",&N);
  54. for(int i=;i<N;i++){
  55. scanf("%lf%lf",&pt[i].X,&pt[i].Y);
  56. }
  57. POINT ans = getCenter(pt,N);
  58. printf("%.2lf %.2lf\n",ans.X+EPS,ans.Y+EPS);
  59. }
  60. return ;
  61. }

[POJ 1385] Lifting the Stone (计算几何)的更多相关文章

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

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

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

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

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

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

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

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

  5. Lifting the Stone(hdoj1115)

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

  6. (hdu step 7.1.3)Lifting the Stone(求凸多边形的重心)

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

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

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

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

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

  9. Lifting the Stone(hdu1115)多边形的重心

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

随机推荐

  1. 学习Python遇到的那些坑

    1. 初始化一个类,这个方法名必须为”__init__(object)“.顺便提一下,两边的下划线是均是2个 2. 每个程序块都要使用冒号!!!! 3. 如果程序中使用了非英文字符,需要在Python ...

  2. com.opensymphony.module.sitemesh.filter.PageFilter 装饰页面

    1.web.xml中配置: <filter> <filter-name>sitemeshFilter</filter-name> <filter-class& ...

  3. Http状态码301和302概念简单区别

    1.什么是301重定向? 301重定向/跳转一般,表示本网页永久性转移到另一个地址. 301是永久性转移(Permanently Moved),SEO常用的招式,会把旧页面的PR等信息转移到新页面: ...

  4. 06socket编程

    socket可以看成是用户进程与内核网络协议栈的编程接口. socket不仅可以用于本机的进程间通信,还可以用于网络上不同主机的进程间通信. IPv4套接口地址结构通常也称为“网际套接字地址结构”,它 ...

  5. java 反射机制01

    // */ // ]]>   java反射机制01 Table of Contents 1 反射机制 2 反射成员 2.1 java.lang.Class 2.2 Constructor 2.3 ...

  6. 【转】Asp.Net MVC3 简单入门详解过滤器Filter

    原文地址:http://www.cnblogs.com/boruipower/archive/2012/11/18/2775924.html 前言 在开发大项目的时候总会有相关的AOP面向切面编程的组 ...

  7. Dell R410 broadcom网卡驱动更新失败

    问题描述: 最近遇到一个Dell R410 broadcom网卡驱动更新失败的问题.从官网上下载的驱动在安装的过程中都会自己回滚回来,很是困惑. 尝试解决: Dell官网现在提供的驱动一般最少有两种格 ...

  8. Python 迭代删除重复项,集合删除重复项

    1. 迭代删除重复项:先排序列表项,然后通过新迭代(not in)去除重复项,分片打印 def sanitize(time_string): if '-' in time_string: splitt ...

  9. showdialog窗体不在任务栏显示的问题处理

    场景: c#开发的windows窗体用showdialog弹出时,在任务栏中 win7系统显示,win xp和win 2003却不显示. 窗体的ShowInTaskbar已设置为True. 解决: 在 ...

  10. oracle学习笔记(二)设置归档模式

    设置归档模式(mount状态) ALTER database ARCHIVELOG; //关闭数据库 shutdown immediate //启动数据库到mount状态 startup mount ...