Hdoj 1115.Lifting the Stone 题解
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
思路
求多边形重心步骤如下:
- 将原N多边形划分为N-2个三角形
- 求每个三角形的重心和面积:重心是坐标和/3,面积用叉乘处理
- 多边形的重心为\(G_x = \frac{\sum x_i*area_i}{3 * \sum area_i}\),\(G_y = \frac{\sum y_i*area_i}{3 * \sum area_i}\)
代码
#include<bits/stdc++.h>
using namespace std;
struct node
{
int x;
int y;
} a[1000010];
double crossMult(node a,node b)
{
return a.x*b.y-a.y*b.x;
}
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
node a,b,c;
cin >> n;
cin >> a.x >> a.y >> b.x >> b.y;
n -= 2;//已经读了2个点
double area = 0.0;
double sum_xs = 0.0,sum_ys = 0.0;
while(n--)
{
cin >> c.x >> c.y;
node newb,newc;
newb.x = b.x - a.x; newb.y = b.y - a.y;
newc.x = c.x - a.x; newc.y = c.y - a.y;
double tmp_area = crossMult(newb,newc)/2.0;
area += tmp_area;
sum_xs += ((a.x + b.x + c.x) * tmp_area);
sum_ys += ((a.y + b.y + c.y) * tmp_area);
b = c;
}
printf("%.2lf %.2lf\n",sum_xs/area/3.0,sum_ys/area/3.0);
}
return 0;
}
Hdoj 1115.Lifting the Stone 题解的更多相关文章
- poj 1115 Lifting the Stone 计算多边形的中心
Lifting the Stone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- hdu 1115:Lifting the Stone(计算几何,求多边形重心。 过年好!)
Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1115 Lifting the Stone 多边形的重心
Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1115 Lifting the Stone (数学几何)
Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1115 Lifting the Stone
题目链接:hdu 1115 计算几何求多边形的重心,弄清算法后就是裸题了,这儿有篇博客写得很不错的: 计算几何-多边形的重心 代码如下: #include<cstdio> #include ...
- hdu1115 Lifting the Stone(几何,求多边形重心模板题)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1115">http://acm.hdu.edu.cn/showproblem.php ...
- Lifting the Stone(多边形重心)
Lifting the Stone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- POJ 1385 Lifting the Stone (多边形的重心)
Lifting the Stone 题目链接: http://acm.hust.edu.cn/vjudge/contest/130510#problem/G Description There are ...
- Lifting the Stone(hdoj1115)
Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- Mysql 字符集及排序规则
一.字符集 字符集:就是用来定义字符在数据库中的编码的集合. 常见的字符集:utf8.Unicode.GBK.GB2312(支持中文).ASCCI(不支持中文) 二.字符集排序规则 作者本人用 ...
- Linux之hosts文件
一.序言: 今天同事部署环境遇到问题, 原因1:修改了主机名,在/etc/hosts文件中加了3台集群的ip和主机名,但是将默认的前两行也改了,没注意看改了哪里, 现象: 1.zookeeper单台可 ...
- Vmware的虚拟机示例进入BIOS方法
虚拟机(Vmware)怎么进入BIOS_百度经验 https://jingyan.baidu.com/article/7e440953e566472fc0e2eff7.html Vmware虚拟机进入 ...
- 获取环境变量,0x000000cb 操作系统找不到已输入的环境选项
include "stdafx.h" #include <Windows.h> #include <iostream> #pragma warning(di ...
- Composer之搭建自己的包工具
作为一个标准的PHPer,必须学会优雅的使用composer,最近,萌生了一个想法,我们每搭建一个项目,里面都会有许多的公用的方法和类库,每次使用的时候就是将其拷贝过来,或者重新写一遍,过于繁琐,效率 ...
- 日志分析工具之goAccess
在此推荐一款分析日志的工具,方便我们日常对于网站的访问状况有一个较为清晰的了解 一.安装 官网: https://goaccess.io/download 源码安装: 1. wget http:// ...
- js this的含义以及讲解
this关键字是一个非常重要的语法点.毫不夸张地说,不理解它的含义,大部分开发任务都无法完成. 首先,this总是返回一个对象,简单说,就是返回属性或方法“当前”所在的对象. 下面来两个例子来让大家更 ...
- babel(一)
一.babel npm babel src/index.js -d lib 二.@babel/core @babel/cli @babel/core 转换语法核心 @babel/cli 执行 ...
- css中如何做到容器按比例缩放
需求: 一般在响应式中,我们会要求视频的宽高比为16:9或4:3,这么一来就比较头大了.当用户改变浏览器宽度的时候(改变高度不考虑),视频的宽度变了,那么高度也得根据我们要求的16:9或4:3改变. ...
- RPC框架-RMI、RPC和CORBA的区别
关键词:RMI RPC CORBA简 介:本篇文章重点阐述RMI,附带介绍RPC和CORBA Java远程方法调用(Java RMI)是一组实现了远程方法调用(rmi)的API. java RMI是远 ...