zoukankan      html  css  js  c++  java
  • 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

    Central Europe 1999


    思路

    求多边形重心步骤如下:

    • 将原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
    ",sum_xs/area/3.0,sum_ys/area/3.0);
    		
    	}
    	return 0;
    }
    
  • 相关阅读:
    vue+element实现图片上传
    react----Hooks的基本使用
    js知识点大杂烩
    vue面试题(一)
    从一个字符串中找出重复次数最多的字符?
    百度搜索
    js--拖拽
    js循环嵌套,打印图形
    js--sort()排序
    闰秒调整扫盲
  • 原文地址:https://www.cnblogs.com/MartinLwx/p/10075351.html
Copyright © 2011-2022 走看看