CERC 1999 (SPOJ 40) Lifting the Stone

29 篇文章 0 订阅
3 篇文章 0 订阅

Link To The Problem


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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值