现在的位置: 首页 > 综合 > 正文

HDOJ/HDU 1115

2017年11月22日 ⁄ 综合 ⁄ 共 2908字 ⁄ 字号 评论关闭

 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1115

Lifting the Stone

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

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
 

直接套用模板,不过不知道为什么最后精度丢失了。。。。WA了许久。。

 

我的代码:

#include<stdio.h>
#include<math.h>
#define eps 1e-8

struct point
{
	double x;
	double y;
};
struct line
{
	point a;
	point b;
};
point p[1000005];

double xmult(point p1,point p2,point p0)
{
	return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

point intersection(line u,line v)
{
	point ret=u.a;
	double t=((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x))/((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));
	ret.x=ret.x+(u.b.x-u.a.x)*t;
	ret.y=ret.y+(u.b.y-u.a.y)*t;
	return ret;
}

point barycenter(point a,point b,point c)
{
	line u,v;
	u.a.x=(a.x+b.x)/2;
	u.a.y=(a.y+b.y)/2;
	u.b=c;
	v.a.x=(a.x+c.x)/2;
	v.a.y=(a.y+c.y)/2;
	v.b=b;
	return intersection(u,v);
}

point barycenter(int n)
{
	point ret,t;
	double t1=0,t2;
	int i;
	ret.x=ret.y=0;
	for(i=1;i<n-1;i++)
	{
		if(fabs(t2=xmult(p[0],p[i],p[i+1]))>eps)
		{
			t=barycenter(p[0],p[i],p[i+1]);
			ret.x=ret.x+t.x*t2;
			ret.y=ret.y+t.y*t2;
			t1=t1+t2;
		}
	}
	if(fabs(t1)>eps)
	{
		ret.x=ret.x/t1;
		ret.y=ret.y/t1;
	}
	return ret;
}

int main()
{
	int t,n,i;
	point center;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(i=0;i<n;i++)
			scanf("%lf%lf",&p[i].x,&p[i].y);
		center=barycenter(n);
		printf("%.2lf %.2lf\n",(center.x+0.001),(center.y+0.001));
	}
	return 0;
}

 

抱歉!评论已关闭.