HDU 1115 lifting the stone

Source: Internet
Author: User
Lifting the stone

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/others) total submission (s): 5440 accepted submission (s): 2278

Problem descriptionthere are eclipsecret openings in the floor which are covered by a big heavy stone. when the stone is lifted up, a special mechanic detects this and activates initialize ONED 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 B E lifted all at once; no side can rise before another. so it is very important to find the center 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 center of gravity for the given polygon. inputthe 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 EDG Es never touch each other (except t 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. outputprint exactly one line for each test case. the line shoshould contain exactly two numbers separated by one space. these numbers are the coordinates of the center 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 center 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 center anyway. sample input2 4 5 0 0 5-5 0-5 4 1 1 11 1 11 11 11 1 11 sample output0.00 0.00 6.00 6.00 this question is the question of finding the polygon center of gravity. You can create a template.

The vertices in this question are given in a counter-clockwise order.

The polygon center of gravity is divided into two situations:

① Quality is concentrated on the vertex. The coordinates of N vertices are (XI, Yi), and the weight is Mi, then the center of gravity

X = sigma (XI x mi)/SIGMA Mi Y = sigma (yi x mi)/SIGMA Mi

In particular, if the quality of each vertex is the same, x = Σ XI/n y = Σ Yi/n

② Uniform quality distribution. This is the type of question, and the algorithm is different from the above.

In particular, the triangle center of gravity with uniform quality:

X = (x0 + X1 + x2)/3 Y = (y0 + Y1 + y2)/3

 

Question reference from: http://blog.csdn.net/lttree/article/details/24720007

 

Therefore, this question solution:

1. Take a vertex as the vertex and make multiple triangles, and then find the center of gravity and quality of each triangle.

2. Then, use the center of gravity of each triangle as the vertex to form a polygon.

3. This polygon belongs to the first case above. The Polygon Area of the quality on the vertex.

4. Set the formula and click OK ~

PS: Notes:

Because the triangle quality is proportional to the area (why? Because the quality distribution is even ~.~), Therefore, quality can be replaced by available area.

In addition, if you use cross product to calculate the Triangle Area, you must calculate the positive and negative conditions (the positive and negative numbers must be retained ),

To prevent a polygon from being a concave polygon, the area of the polygon is outside the polygon.

OK ~ Cuihua, template ~, This template will also be updated in the computing geometric template I have compiled ~~~

#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <vector>#define x first#define y secondusing namespace std;typedef long long LL;typedef pair<double,double> Point;const int N = 10010;int n;vector<Point>P;Point cal(){    Point p ,s ;    double tp , area = 0 , tpx = 0 ,tpy = 0;    p.x = P[0].x ,p.y = P[0].y;    for( int i = 1 ; i <= n  ;++i )    {        if(i == n ) s.x = P[0].x , s.y = P[0].y;        else s.x = P[i].x , s.y = P[i].y;        tp = ( p.x * s.y - p.y *s.x );        area += tp / 2.0 ;        tpx += ( p.x + s.x )* tp ;        tpy += ( p.y + s.y )* tp ;        p.x =s.x ; p.y = s.y;    }    s.x = tpx / (6*area);    s.y = tpy / (6*area);    return s;}void run(){    double xx,yy;    P.clear();    scanf("%d",&n);    for(int i = 0 ; i< n ;++i){        scanf("%lf%lf",&xx,&yy);        P.push_back(Point(xx,yy));    }    Point res = cal();    printf("%.2lf %.2lf\n",res.x,res.y);}int main(){    #ifdef LOCAL//        cout<<"1"<<endl;        freopen("in.txt","r",stdin);    #endif // LOCAL    int cas =1 ,_;    cin>>_;    while(_--)    {        run();    }    return 0;}

 

 

HDU 1115 lifting the stone

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.