bzoj-1132 Tro

Source: Internet
Author: User

Test instructions

Given n points, the area of all triangles consisting of these n points is calculated;

n<=3000;


Exercises

This problem O (N^3) enumeration triangle time complexity is unbearable;

So consider enumerating an edge, multiple triangles to calculate, complexity at O (n^2) level;

The triangular area can be used to calculate the height of the area, or the cross-product;

If the method of the bottom multiply height is used to find out the sum of all the points to the straight line distance, it is also possible to get the current solution by O (1);

But the distance and this step must be O (n), it is difficult to transfer to the next bottom up;

And the method of cross product is to simplify the cross product of the formula, you can put the coordinates of points, plus and a calculation;

But the cross product is to have the direction area, this thing card me half a day;

The actual implementation is this:

Enumerates the first point, takes the point set at the top right of the first point, sorts the point set by the slope size of the first point, and then enumerates the second point for solving;

In this way each area is only one time, and because of the slope is orderly, so all triangles are positive area;

Because of the order, Time complexity O (n^2 logn);

(I can't believe I kept two decimal places in WA for one night!!) )


Code:


#include <math.h> #include <stdio.h> #include <string.h> #include <algorithm> #define N 3100using namespace Std;typedef Long long ll;struct point{ll x,y;double slope;friend bool operator < (point A,point b) {i F (a.x==b.x) return A.y<b.y;return a.x<b.x;} Friend point operator-(point A,point B) {return (point) {a.x-b.x,a.y-b.y};} Friend int operator * (Point A,point b) {return a.x*b.y-a.y*b.x;}} A[n],t[n],o;bool CMP (point A,point B) {return a.slope>b.slope;} int main () {int n,m,i,j,k;ll ans,sx,sy,tx,ty;scanf ("%d", &n), for (i=1,sx=sy=0;i<=n;i++) {scanf ("%d%d", &a[i ].X,&AMP;A[I].Y); sx+=a[i].x,sy+=a[i].y;} Sort (a+1,a+1+n); for (i=1,ans=0;i<=n;i++) {sx-=a[i].x,sy-=a[i].y;tx=sx,ty=sy;o=a[i];memcpy (t+i+1,a+i+1,sizeof ( Point) * (N-i)), for (j=i+1;j<=n;j++) {if (t[j].x==o.x) t[j].slope=1e10;elset[j].slope= (double) (T[J].Y-O.Y)/(T[j]. x-o.x);} Sort (t+i+1,t+n+1,cmp); for (j=i+1;j<n;j++) {point v=t[j]-a[i];tx-=t[j].x,ty-=t[j].y;ans+=tx*v.y-ty*v.x+ (n-j) * (A [I].y*v.x-a[i].x*v.y);}} printf"%lld.%d", ans>>1,ans&1?5:0); return 0;} 




bzoj-1132 Tro

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.