hdoj-1115-Lifting the Stone 求多边形重心问题

Lifting the Stone

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6329 Accepted Submission(s): 2624

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

Central Europe 1999

Recommend

Eddy | We have carefully selected several similar problems for you: 1392 1086 2108 2150 1348

#include<stdio.h>
#include<math.h>
struct p{
	double x,y;
};
double cross(p a,p b,p c){
	return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n;
		p p1,p2,tp;
		double res=0,s,tx=0,ty=0;
		scanf("%d",&n);
		scanf("%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y);
		for(int i=3;i<=n;++i){
			scanf("%lf%lf",&tp.x,&tp.y);

		    s=cross(p1,p2,tp);
			res+=s;

			tx+=(p1.x+p2.x+tp.x)*s;
			ty+=(p1.y+p2.y+tp.y)*s;
			p2.x=tp.x;p2.y=tp.y;
		}
		printf("%.2lf %.2lf\n",tx/(3*res),ty/(3*res));
	}
	return 0;

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-29 00:47:15

hdoj-1115-Lifting the Stone 求多边形重心问题的相关文章

HDU 1115 Lifting the Stone (求多边形的重心)

题目链接:传送门 分析: 求多边形的重心的方法:传送门 代码如下: #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> using namespace std; const double eps = 1e-10; struct Point{ double x,y; Point():x(0),y(0){} Poi

HDOJ 1115 Lifting the Stone 多边形重心

来自:http://blog.csdn.net/tiaotiaoyly/article/details/2087498 1,质量集中在顶点上.n个顶点坐标为(xi,yi),质量为mi,则重心 X = ∑( xi×mi ) / ∑mi Y = ∑( yi×mi ) / ∑mi 特殊地,若每个点的质量相同,则 X = ∑xi  / n Y = ∑yi  / n 2,质量分布均匀.这个题就是这一类型,算法和上面的不同. 特殊地,质量均匀的三角形重心: X = ( x0 + x1 + x2 ) / 3

hdu1115 Lifting the Stone(几何,求多边形重心模板题)

题意:就是给你一个多边行的点的坐标,求此多边形的重心. 一道求多边形重心的模板题! #include<cstdio> #include<cmath> #include<cstring> using namespace std; struct point { double x,y; }PP[1000047]; point bcenter(point pnt[],int n){ point p,s; double tp,area = 0, tpx=0, tpy=0; p.x

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

Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5370 Accepted Submission(s): 2239 Problem Description There are many secret openings in the floor which are covered by a big heavy

hdu 1115 Lifting the Stone (数学几何)

Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5203    Accepted Submission(s): 2155 Problem Description There are many secret openings in the floor which are covered by a big

HDU 1115 Lifting the Stone

Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5440    Accepted Submission(s): 2278 Problem Description There are many secret openings in the floor which are covered by a big

hdu3685 Rotational Painting 求多边形重心和凸包

http://acm.hdu.edu.cn/showproblem.php?pid=3685 Rotational Painting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2614    Accepted Submission(s): 737 Problem Description Josh Lyman is a gifted

UVALive 4426 Blast the Enemy! --求多边形重心

题意:求一个不规则简单多边形的重心. 解法:多边形的重心就是所有三角形的重心对面积的加权平均数. 关于求多边形重心的文章: 求多边形重心 用叉积搞一搞就行了. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #define Mod 100000000

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

题目: Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 168 Accepted Submission(s): 98   Problem Description There are many secret openings in the floor which are covered by a big he