Hdu 4697 Convex hull uses cross product allocation rate for moving Convex hull credits

Source: Internet
Author: User
Tags time 0

Point n and time T for moving convex points. Point 0 is p, and the speed of each point (given by a vector) is v, ask you the average area of each unit of time. Idea: for this type of questions, it is easy to think of time segmentation for Convex packets. 1. So we first find all the time points, that is, when there are three colons in the point set, it is a time point. For the three I, j, and k vertices, the cross product can be used to list a one-dimensional quadratic equation (maybe a = 0) About time t, and solve all the time points. Attention should be paid here. It is easy to prove that the cross product satisfies the allocation rate. Therefore, you define a cross-Multiplication operation in the struct point, which is very convenient for finding the coefficient of the equation and is not prone to errors. Of course, for time 0 and T, it is also a time point 2. enumerate all adjacent time periods, take the intermediate time in the time period, calculate a convex packet for this time period, and calculate the point of the current time on the convex packet. Then we use these points to calculate the area. The area is a function about time t, which is a * x ^ 2 + B * x + c. The method for calculating the area is as follows: take the vertex on a convex bag as the center and enumerate all adjacent two points for cross product. It is similar to the equation for finding a time point. The accumulation coefficients are a, B, and c. Point a/3 * x ^ 3 + B/2 * x ^ 2 + c * x, and accumulate the time between the two ends into the answer. Note that the area here is a directed area, which has been pitted for a long time. Flexible use of the distribution rate of cross product can quickly solve this problem.

# Include <cstdio> # include <cstring> # include <cmath> # include <algorithm> using namespace std; const double eps = 1e-9; inline int dcmp (double x) {if (fabs (x) <eps) return 0; return x> eps? 1:-1;} struct point {double x, y; int id; inline void in (int I) {scanf ("% lf", & x, & y); id = I;} point (double x = 0, double y = 0): x (x), y (y) {} inline point operator-(const point & t) const {return point (x-t. x, y-t. y);} inline point operator + (const point & t) const {return point (x + t. x, y + t. y);} point operator/(const double & t) const {return point (x/t, y/t);} point op Erator * (const double & t) const {return point (x * t, y * t);} inline double operator * (const point & t) const {// return x * t. y-y * t. x;} bool operator <(const point & t) const {if (y = t. y) return x <t. x; return y <t. y ;}} p [55], v [55], tp [55], st [55]; double t [55*55*55], T, cur, ans; int sz, n, m; double a, B, c; inline double cross (const point & o, const point & a, const point & B) {return (. X-o.x) * (B. y-o.y)-(. y-o.y) * (B. x-o.x);} int graham (point * p, int n, point * st) {// convex hull sort (p, p + n); int top = 0; int I; for (I = 0; I <n; I ++) {while (top> = 2 & cross (st [top-2], st [top-1], p [I]) <eps) top --; st [top ++] = p [I];} int t = top + 1; for (I = n-2; i> = 0; I --) {while (top> = t & cross (st [top-2], st [top-1], p [I]) <eps) top --; st [top ++] = p [I];} return top;} inline Void gao (int I, int j, int k) {// function for coefficient calculation point a1 = p [I]-p [j], a2 = p [I]-p [k]; point b1 = v [I]-v [j], b2 = v [I]-v [k]; a + = b1 * b2; B + = a1 * b2 + b1 * a2; c + = a1 * a2;} inline void solve (double a, double B, double c) {// to solve the equation, pay attention to the case of a = 0 double x; if (a = 0) {if (B! = 0) {x =-c/B; if (x> = 0 & x <= T) t [sz ++] = x;} return ;} double dlt = B * B-4 * a * c; if (dlt <0) return; if (dlt = 0) {x =-B * 0.5/; if (x> = 0 & x <= T) t [sz ++] = x; return;} dlt = sqrt (dlt ); x = 0.5 * (-B + dlt)/a; if (x> = 0 & x <= T) t [sz ++] = x; x = 0.5 * (-B-dlt)/a; if (x> = 0 & x <= T) t [sz ++] = x ;} inline double F (double x) {// returns a * x/3.0 + B * x/2.0 + c * x ;} int main () {int I, j, K; while (~ Scanf ("% d % lf", & n, & T) {for (I = 0; I <n; I ++) p [I]. in (I), v [I]. in (I); if (n <= 2) {printf ("%. 10f \ n ", 0.0); continue;} t [0] = 0; t [1] = T; // process all time points sz = 2; for (I = 0; I <n; I ++) for (j = I + 1; j <n; j ++) for (k = j + 1; k <n; k ++) {a = B = c = 0; gao (I, j, k); solve (a, B, c);} sort (t, t + sz); ans = 0; for (I = 0; I <sz-1; I ++) {// enumerate the adjacent time periods cur = 0.5 * (t [I] + t [I + 1]); a = B = c = 0; for (j = 0; j <n; j ++) {tp [j] = p [j] + v [j] * cur; tp [j]. id = p [j]. id;} m = graham (tp, n, st); // obtain the point on the convex hull for (j = 2; j <m; j ++) gao (st [0]. id, st [J-1]. id, st [j]. id); // calculate the coefficient ans + = F (t [I + 1])-F (t [I]); // calculate the area manually} printf ("%. 10f \ n ", fabs (ans * 0.5/T);} return 0 ;}

 


Related Article

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.