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

题目链接

求一个n个顶点多边形的重心。

将多边形分解为n-2个三角形,分别求其重心和面积(因为质量分布均匀),多边形的重心为:

X = x i × m i m i X=\frac{\sum{x_{i}}\times{m_{i}}}{\sum{m_{i}}} X=mixi×mi Y = y i × m i m i Y=\frac{\sum{{y_{i}}\times{m_{i}}}}{\sum{m_{i}}} Y=miyi×mi

这里三角形面积由叉乘求出,由于叉乘的有向性所以可以将原点当做基准点和多边形上任意两点组成三角形叉积算面积求和(这样向量坐标即为顶点坐标)。

注意尽量减少除法的使用以减少精度损失,题目要求四舍五入所以最后+0.001以确保2.555可以入上。

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
#define XDebug(x) cout<<#x<<"="<<x<<endl;
#define ArrayDebug(x,i) cout<<#x<<"["<<i<<"]="<<x[i]<<endl;
#define print(x) out(x);putchar('\n');
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
typedef pair<ll,ll> PLL;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;
template <class T>
inline bool read(T &ret) {
	char c;
	int sgn;
	if (c = getchar(), c == EOF) {
		return 0;
	}
	while (c != '-' && (c < '0' || c > '9')) {
		c = getchar();
	}
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0' && c <= '9') {
		ret = ret * 10 + (c - '0');
	}
	ret *= sgn;
	return 1;
}
template <class T>
inline void out(T x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x > 9) {
		out(x / 10);
	}
	putchar(x % 10 + '0');
}

struct Point {
	double x, y;
	Point(double _x = 0.0, double _y = 0.0): x(_x), y(_y) {}
	void input() {
		scanf("%lf%lf", &x, &y);
	}
	void output() {
		printf("%.2lf %.2lf\n", x, y);
	}
	Point operator - (const Point &b) const{
		return Point{x - b.x, y - b.y};
	}
	double operator ^ (const Point &b) const {
		return x * b.y - y * b.x;
	}
};

struct Triangle {
	double GravityX, GravityY, Square;
	Triangle(double _GravityX = 0, double _GravityY = 0, double _Square = 0): GravityX(_GravityX), GravityY(_GravityY), Square(_Square) {}
};

double PointDis(Point a, Point b) {
	return hypot(a.x - b.x, a.y - b.y);
}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	int T;
	read(T);
	for (int Case = 1, N; Case <= T; ++Case) {
		read(N);
		vector<Point> points(N);
		vector<Triangle> triangles(N);
		Point Gravity;
		double Square = 0.0;
		for (int i = 0; i < N; ++i) {
			points[i].input();
		}
		points.pb(points[0]);
		for (int i = 0; i < N; ++i) {
			triangles[i].GravityX = (points[i].x + points[i + 1].x);
			triangles[i].GravityY = (points[i].y + points[i + 1].y);
			triangles[i].Square = (points[i] ^ points[i + 1]) / 2;
			Square += triangles[i].Square;
		}
		for (auto i : triangles) {
			Gravity.x += i.GravityX * i.Square;
			Gravity.y += i.GravityY * i.Square;
		}
		Gravity.x = Gravity.x / Square / 3; Gravity.y = Gravity.y / Square / 3;
		Gravity.x += 0.001, Gravity.y += 0.001;
		Gravity.output();
	}
#ifndef ONLINE_JUDGE
	fclose(stdin);
	fclose(stdout);
	system("gedit out.txt");
#endif
    return 0;
}