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
 
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 1000009
#define N 21
#define MOD 1000000
#define INF 1000000009
const double eps = 1e-;
const double PI = acos(-1.0);
/*
所有线段投射到给定线段上取交集,如果交集距离大于eps 存在!s
*/
int sgn(double x)
{
if (fabs(x) < eps) return ;
if (x < ) return -;
else return ;
}
struct Point
{
double x, y;
Point() {}
Point(double _x, double _y) :x(_x), y(_y) {}
Point operator - (const Point& r)const
{
return Point(x - r.x, y - r.y);
}
double operator ^(const Point& r)const
{
return x*r.y - y*r.x;
}
double operator * (const Point& r)const
{
return x*r.x + y*r.y;
}
};
double dist(Point a, Point b)
{
return sqrt((a - b)*(a - b));
}
struct Line
{
Point s, e;
Line() {}
Line(Point _a, Point _B) :s(_a), e(_B) {}
};
bool Seg_inter_line(Line l1, Line l2)
{
return sgn((l2.s - l1.e) ^ (l1.s - l1.e))*sgn((l2.e - l1.e) ^ (l1.s - l1.e)) <= ;
}
bool cross(Line l1, Line l2)
{
return
max(l1.s.x, l1.e.x) >= min(l2.s.x, l2.e.x) &&
max(l2.s.x, l2.e.x) >= min(l1.s.x, l1.e.x) &&
max(l1.s.y, l1.e.y) >= min(l2.s.y, l2.e.y) &&
max(l2.s.y, l2.e.y) >= min(l1.s.y, l1.e.y) &&
sgn((l2.s - l1.e) ^ (l1.s - l1.e))*sgn((l2.e - l1.e) ^ (l1.s - l1.e)) <= &&
sgn((l1.s - l2.e) ^ (l2.s - l2.e))*sgn((l1.e - l2.e) ^ (l2.s - l2.e)) <= ;
}
Point a[MAXN];
double CalcArea(Point p[], int n)
{
double res = ;
for (int i = ; i < n; i++)
res += (p[i] ^ p[(i + ) % n]) / ;
return fabs(res);
}
bool isconvex(Point p[], int n)
{
bool s[];
memset(s, false, sizeof(s));
for (int i = ; i < n; i++)
{
s[sgn((p[(i + ) % n] - p[i]) ^ (p[(i + ) % n] - p[i])) + ] = true;
if (s[] && s[])
return false;
}
return true;
}
double ci[MAXN];
Point ti[MAXN];
Point Calgravitycenter(Point p[], int n)
{
Point res(, );
double area = ;
for (int i = ; i < n; i++)
{
ci[i] = (p[i] ^ p[(i + ) % n]) ;
ti[i].x = (p[i].x + p[(i + ) % n].x);
ti[i].y = (p[i].y + p[(i + ) % n].y);
res.x += ti[i].x * ci[i];
res.y += ti[i].y * ci[i];
area += ci[i]/;
}
res.x /= (*area);
res.y /= (*area);
return res;
}
int main()
{
int T,n;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
for (int i = ; i < n; i++)
scanf("%lf%lf", &a[i].x, &a[i].y);
Point ans = Calgravitycenter(a, n);
printf("%.2lf %.2lf\n", ans.x, ans.y);
} }

Lifting the Stone 计算几何 多边形求重心的更多相关文章

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

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...

  2. POJ 1385 Lifting the Stone (多边形的重心)

    Lifting the Stone 题目链接: http://acm.hust.edu.cn/vjudge/contest/130510#problem/G Description There are ...

  3. poj 1115 Lifting the Stone 计算多边形的中心

    Lifting the Stone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  4. 多边形求重心 HDU1115

    http://acm.hdu.edu.cn/showproblem.php?pid=1115 引用博客:https://blog.csdn.net/ysc504/article/details/881 ...

  5. hdu1115【多边形求重心模板】

    1.质量集中在顶点上.n个顶点坐标为(xi,yi),质量为mi,则重心(∑( xi×mi ) / ∑mi, ∑( yi×mi ) / ∑mi) 2.质量分布均匀.这个题就是这一类型,算法和上面的不同. ...

  6. POJ1385 Lifting the Stone 多边形重心

    POJ1385 给定n个顶点 顺序连成多边形 求重心 n<=1e+6 比较裸的重心问题 没有特别数据 由于答案保留两位小数四舍五入 需要+0.0005消除误差 #include<iostr ...

  7. hdu 1115 Lifting the Stone

    题目链接:hdu 1115 计算几何求多边形的重心,弄清算法后就是裸题了,这儿有篇博客写得很不错的: 计算几何-多边形的重心 代码如下: #include<cstdio> #include ...

  8. hdu 1115:Lifting the Stone(计算几何,求多边形重心。 过年好!)

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. Lifting the Stone(求多边形的重心—)

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...

随机推荐

  1. astgo-完整功能介绍

    核心功能: Astgo最核心和强大的功能是呼叫中心模块.接入方式:中继卡.模拟卡接入,中继网关.O口网关接入.网络IP接入等.单机200个坐席,通话实时录音.灵活队列分组.开放式IVR设计,修改业务逻 ...

  2. CentOS Linux VPS桌面环境一键安装包

  3. 直接使用FileSystem以标准输出格式显示hadoop文件系统中的文件

    package com.yoyosys.cebbank.bdap.service.mr; import java.io.IOException; import java.io.InputStream; ...

  4. 详细介绍idea实现javaweb项目登入注册(华东交通大学教务处信息管理系统)、模糊查询

    详细介绍idea实现javaweb项目登入注册(华东交通大学教务处信息管理系统).模糊查询 1,创建数据库,我的用户名:root 密码:root,数据库名称:lianwei,表名:login 2,效果 ...

  5. c++小游戏

    #include <iostream> using namespace std; double shengmingli=2000;//定义主角初始生命力 int gongjili=150; ...

  6. 【洛谷3546_BZOJ2803】[POI2012]PRE-Prefixuffix(String Hash)

    Problem: 洛谷3546 Analysis: I gave up and saw other's solution when I had nearly thought of the method ...

  7. ACM_滚动AC

    滚动AC Time Limit: 2000/1000ms (Java/Others) Problem Description: 小光最近拉了几个同学入ACM的坑,为鼓励A题,就增加奖励制度:每AC三道 ...

  8. 跨平台键鼠共享软件synergy使用

    如果共享的机子都是win系统的话,也可以使用 无界鼠标. 这里主要讲跨平台通用的synergy.下载地址:http://synergy-project.org/ 注意1:最好下载同一位数,同一版本的. ...

  9. Python语言之数据结构2(字典,引用)

    1.字典 键值对. dict={ "key1" : "value1", "key2" : "value2" } #add ...

  10. HDU_1079_思维题

    Calendar Game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...