Solution : 多边形的重心
PS : POJ 神数据就是过不掉,SPOJ上1A.。。(尴尬。。。)
Code:
// CERC 1999 Lifting The Stone
// Solution : 多边形的重心
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define sf scanf
#define pf printf
#define dbg(x) cerr << __LINE__ << " : " << #x << " : " << (x) << endl
#define pb push_back
typedef double D;
D const eps = 1e-9;
int inline dcmp(D x) {
if(x>=-eps && x<=eps) return 0;
return x > 0 ? 1 : -1;
}
struct P {
D x,y;
P() {};
P(D x, D y):x(x),y(y){};
void read() { sf("%lf%lf",&x,&y); }
void out() { pf("%.2lf %.2lf\n",x+eps,y+eps); }
D len() { return sqrt(x*x+y*y); }
D operator * (P u) { return x*u.y - y*u.x; }
P operator / (D k) { return P(x/k,y/k); }
P operator * (D k) { return P(x*k,y*k); }
P operator - (P u) { return P(x-u.x,y-u.y); }
P operator + (P u) { return P(x+u.x,y+u.y); }
};
P Tri(P a, P b, P c) {
return (a+b+c) / 3.0;
}
int S(vector<P> p,P& ret) {
int n = p.size();
p.pb(p[0]);
D sum=0;
ret = P(0,0);
rep(i,n) {
D tmp = (p[i]-P(0,0))*(p[i+1]-p[i]);
ret = ret + Tri(P(0,0) , p[i], p[i+1])*tmp;
sum += tmp;
}
if(dcmp(sum)==0) return 0;
ret = ret / sum;
return 1;
}
P S1(vector<P> p,int n) {
P ret(0,0);
rep(i,n) ret = ret + p[i];
return ret / (D)n;
}
vector<P> p;
int n;
P a;
// Solution Part
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int T;
cin >> T;
while(T--) {
p.clear();
sf("%d",&n);
rep(i,n) { a.read();p.pb(a); }
if(S(p,a)==0) a=S1(p,n);
a.out();
}
return 0;
}