首页 技术 正文
技术 2022年11月18日
0 收藏 777 点赞 2,202 浏览 3389 个字

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 DescriptionThere 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.  InputThe 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.  OutputPrint 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 Input245 00 5-5 00 -541 111 111 111 11 Sample Output0.00 0.006.00 6.00 SourceCentral Europe 1999 RecommendEddy   |   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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,360
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,853