Bzoj 1132 POI2008 Tro Computational geometry

Source: Internet
Author: User

The sum of the areas of all triangles that can be composed of the points on a given plane

First we enumerate each point to establish a planar Cartesian coordinate with this point as the origin and then sort the points on the first to fourth quadrant and the X, Y axis positive half axes according to the slope.

Enumerating the second and third points to do this is an O (n^3) positive timeout But what did we find?

For each point K its contribution to the answer is:

(X1*YK-Y1*XK) + (X2*YK-Y2*XK) +...+ (X_ (k-1) *yk-y_ (k-1) *xk)

= (X1+x2+...+x_ (k-1)) *yk-(Y1+y2+...+y_ (k-1)) *xk

So just maintain a prefix and you can

Time complexity O (N^2LOGN)

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M    3030using namespace std;struct point{int x, y;    Double slope; Point () {} point (int _,int __): X (_), Y (__) {slope=x? (    Double) Y/x:1e10;        } BOOL operator < (const point &y) const {if (x!=y.x) return x < y.x;    Return y < y.y; }}a[m],points[m];int N,tot;long Long Ans;bool Compare (const point &p1,const point &p2) {return P1.slope < P2 . Slope;}    int main () {int i,j;    cin>>n;    for (i=1;i<=n;i++) scanf ("%d%d", &a[i].x,&a[i].y);    Sort (a+1,a+n+1);        for (i=1;i<=n;i++) {tot=0;        Long Long sum_x=0,sum_y=0;        for (j=i+1;j<=n;j++) points[++tot]=point (A[J].X-A[I].X,A[J].Y-A[I].Y);        Sort (points+1,points+tot+1,compare);            for (j=1;j<=tot;j++) {ans+=sum_x*points[j].y-sum_y*points[j].x;         sum_x+=points[j].x;   SUM_Y+=POINTS[J].Y; }} printf ("%lld.%d\n", ans>>1,ans&1?5:0);}


Bzoj 1132 POI2008 Tro Computational geometry

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.