Jdfzoj 1005 multi-side area scan line

Source: Internet
Author: User

The main idea: to give n a convex polygon, to find the area of these polygons and.


Idea: N is less than 10, can be messed up. There is also a better solution, which seems to require only O (N^2LOGN) time to solve. But I do not, want to understand the reference: http://wyfcyx.is-programmer.com/posts/80378.html

Here's the idea of messing about. Since all are convex polygons, then any line perpendicular to the x-axis in the area of the polygon must be a line segment (or Nothing), then we will all the polygons according to the trapezoidal section, the length of each part of the ladder to calculate the width and the total area can be calculated.

Topic Web site See: http://oj.jdfz.com.cn:8081/oldoj/problem.php?id=1005


CODE:

#define _crt_secure_no_warnings #include <cmath> #include <cstdio> #include <cstring> #include < iomanip> #include <iostream> #include <algorithm> #define MAX 20#define EPS 1e-8using namespace std;# Define MAX (a) > (b)? (a):(b) #define MIN (a) (a) < (b)? (a):(B)) #define INRANGE (X,y,c) (c <= y && c >= x) | |  (c <= x && c >= y))          struct point{double x, y;  Point (Double _,double __): X (_), Y (__) {}-point () {}-operator + (const-point &a) const {return-point (x    + a.x,y + a.y);    } Point operator-(const point &a) const {return point (X-A.X,Y-A.Y);    } Point operator * (double A) const {return point (x * a,y * a);    } void Read () {scanf ("%lf%lf", &x,&y);  }}TEMP[1010]; Inline double Calc (const point &p1,const point &p2) {return sqrt ((p1.x-p2.x) * (p1.x-p2.x) + (P1.Y-P2.Y)  * (P1.Y-P2.Y));} Inline Double Cross (const point &AMp;p1,const Point &p2) {return p1.x * p2.y-p1.y * p2.x;}          struct segment{point p1,p2,v;        Segment (Point _,point __,point ___):p 1 (_), P2 (__), V (___) {} Segment () {} bool Onsegment (const point &p) {        if (p1.x = = p2.x) return InRange (P1.Y,P2.Y,P.Y);    Return InRange (p1.x,p2.x,p.x);  }}*src[max][1010],save[max * 1010];int cnt_seg;          struct interval{double x, y; Interval (double _,double __): X (_), Y (__) {} Interval () {} BOOL operator < (const Interval &a) Const {if (        x = = a.x) return y < a.y;    return x < a.x; }}interval[max * 1010];    Inline point getintersection (const Segment &l1,const Segment &l2) {point u = l1.p1-l2.p1;    Double T = Cross (L2.V,U)/Cross (L1.V,L2.V);  return L1.P1 + l1.v * t;}    Inline Segment *makesegment (const point &p1,const point &p2) {save[++cnt_seg] = Segment (P1,P2,P2-P1);  return &save[cnt_seg];} Inline Interval getinterval (Segment *src[],double x) {Interval Re (0,0); for (int i = 1; src[i]! = NULL; ++i) {Point intersection = Getintersection (*src[i],segment (Point (x,0), point (x,0), Po        Int (0,1)));            if (Src[i]->onsegment (intersection)) {if (!re.x) re.x = INTERSECTION.Y;        else re.y = INTERSECTION.Y;    }} if (Re.x > Re.y) swap (RE.X,RE.Y);  return re;}  int cnt;double divide[1010 * 1010];    int main () {CIN >> cnt;        for (int points,i = 1; I <= cnt; ++i) {scanf ("%d", &points); for (int j = 1; j <= points; ++j) temp[j].        Read ();        for (int j = 1; j < points; ++j) Src[i][j] = makesegment (temp[j],temp[j + 1]);    Src[i][points] = makesegment (temp[points],temp[1]);    } int divides = 0; for (int i = 1; I <= cnt_seg; ++i) for (int j = i + 1; j <= cnt_seg; ++j) {if (Fabs (SAVE[I].V,            SAVE[J].V) < EPS) continue;     Point intersection = Getintersection (Save[i],save[j]);       Divide[++divides] = intersection.x;    } sort (divide + 1,divide + divides + 1);    Double area =. 0;        for (int i = 1; i < divides; ++i) {Double x = (Divide[i + 1] + divide[i])/2;        int intervals = 0;        for (int j = 1; j <= cnt; ++j) Interval[++intervals] = Getinterval (src[j],x);                  Sort (interval + 1,interval + intervals + 1);        Double now =. 0,l = Interval[1].x,r = Interval[1].y;            for (int j = 2; J <= intervals; ++j) if (interval[j].x <= r) r = Max (R,INTERVAL[J].Y);                else {now + = R-l;                L = interval[j].x;            R = interval[j].y;        } now + = R-l;    Area + = Now * (divide[i + 1]-divide[i]);    } cout << fixed << setprecision (3) << area << Endl; return 0;}


Jdfzoj 1005 multi-side area scan line

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.