博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1115:Lifting the Stone(计算几何,求多边形重心。 过年好!)
阅读量:6001 次
发布时间:2019-06-20

本文共 4408 字,大约阅读时间需要 14 分钟。

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:            

 
  计算几何中的水题,求多边形重心。
  第一遍直接套用了模板,因为这几天过年,现在刚回家,回来时候已经很晚了也没时间仔细看具体的做法,过段时间再做一遍。
  过年了,回想这一年,最大的收获就是体会到坚持一样东西的重要性。我会继续努力,不让自己失望,也不让看着我的人失望。马年,继续加油!
 
  给大家拜个晚年!祝大家都能在新的一年里幸福,如意!马年吉祥!!
 
  上代码:
1 #include 
2 #include
3 using namespace std; 4 struct point { double x, y; }; 5 point bcenter(point pnt[], int n){ 6 point p, s; 7 double tp, area = 0, tpx = 0, tpy = 0; 8 p.x = pnt[0].x; p.y = pnt[0].y; 9 for (int i = 1; i <= n; ++i) { // point: 0 ~ n-110 s.x = pnt[(i == n) ? 0 : i].x;11 s.y = pnt[(i == n) ? 0 : i].y;12 tp = (p.x * s.y - s.x * p.y); area += tp / 2;13 tpx += (p.x + s.x) * tp; tpy += (p.y + s.y) * tp;14 p.x = s.x; p.y = s.y;15 }16 s.x = tpx / (6 * area); s.y = tpy / (6 * area);17 return s;18 }19 point P[1000000];20 int main()21 {22 int T,N;23 cin>>T;24 cout<
<
>N;27 for(int i=0;i
>P[i].x>>P[i].y;29 point t = bcenter(P,N); //返回重心坐标 30 cout<
<<' '<
<

 

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

  hdu上AC的代码:

1 #include 
2 typedef struct { 3 double x,y; 4 }Point; 5 double getS(Point a,Point b,Point c) //返回三角形面积 6 { 7 return ((b.x - a.x) * (c.y - a.y) - (b.y - a.y)*(c.x - a.x))/2; 8 } 9 Point getPZ(Point p[],int n) //返回多边形重心10 {11 Point z;12 double sumx = 0,sumy = 0;13 double sumS = 0;14 for(int i=2;i<=n-1;i++){15 double S = getS(p[1],p[i+1],p[i]);16 sumS += S;17 sumx += (p[1].x+p[i].x+p[i+1].x)*S;18 sumy += (p[1].y+p[i].y+p[i+1].y)*S;19 }20 z.x = sumx / (sumS*3 );21 z.y = sumy / (sumS*3 );22 return z;23 }24 Point p[1000001];25 int main()26 {27 int T;28 scanf("%d",&T);29 while(T--){30 int n;31 scanf("%d",&n);32 for(int i=1;i<=n;i++)33 scanf("%lf%lf",&p[i].x,&p[i].y);34 Point z = getPZ(p,n);35 printf("%.2lf %.2lf\n",z.x,z.y);36 }37 return 0;38 }

 

Freecode :

转载地址:http://jpbmx.baihongyu.com/

你可能感兴趣的文章
English - in the light of(按照,根据)与according to的区别是什么
查看>>
浅析linux内核中的idr机制
查看>>
【转】.so兼容32位和64位
查看>>
PowerDesigner跟表的字段加注释
查看>>
w !sudo tee %
查看>>
javascript面试题:如何把一句英文每个单词首字母大写?
查看>>
URAL 1962 In Chinese Restaurant 数学
查看>>
计算 TPS,QPS 的方式
查看>>
洛谷⑨月月赛Round2 P3393逃离僵尸岛[最短路]
查看>>
群晖NAS使用Docker安装迅雷离线下载出现the active key is not valid.
查看>>
spring boot 2使用Mybatis多表关联查询
查看>>
Making HTTP requests via telnet - Tony's Place
查看>>
千元机市场再添“新宠”,红米Note7和vivo Z3谁才是千元王者
查看>>
荣耀10GT升级EMUI 9.0体验分享:这可能是最好用的手机操作系统
查看>>
ZStack基于华芯通打造ARM国产云平台 助力云上贵州多项应用
查看>>
200本“保护日记”记录黄山迎客松生长变化
查看>>
多方力量携手呵护“中华水塔”青海三江源
查看>>
互联网的下一波红利在哪里?
查看>>
拿姐姐身份证登记结婚竟然成了!婚姻户籍信息共享难在哪儿
查看>>
恒大造车加速,联手柯尼塞格打造顶级新能源车
查看>>