Computational Geometry PA1 convex Hull (convex bag)

Source: Internet
Author: User

Title Link: http://dsa.cs.tsinghua.edu.cn/oj/problem.shtml?id=710

CG2015 pa1-1 convex Hull (convex bag) Description (description)

After learning Chapter 1, you must has mastered the convex hull very well. Yes, convex hull is at the kernel of computational geometry and serves as a fundamental geometric structure. That's why you're asked to implement such an algorithm as the first of your programming assignments.

Specifically, given a set of points in the plane, please construct the convex hull and output an encoded description of AL L The extreme points.

After the first chapter of the study, presumably you have a very deep understanding of the convex bag. Yes, convex hull is the core problem of computational geometry and also a basic geometric structure. So your first programming task is to implement such an algorithm.

Specifically, for any set of points on a plane, the corresponding convex hull is constructed and the information of all Poles is output after the encoding conversion.

Input (Inputs)

The first line is a integer n > 0, i.e., the total number of the input points.

The k-th of the following n lines gives the k-th point:

Pk = (xk, yk), k = 1, 2, ..., n

Both xk and yK here is integers and they is delimited by a space.

The first line is a positive integer, the first behavior is a positive whole number n > 0, that is, the total number of input points.

The nth line in the second row is given the K-point:

Pk = (xk, yk), k = 1, 2, ..., n

Here,both x K and yk are integers, and they are separated by a space.

Output (outputs)

let {s< Sub style= "Box-sizing:border-box; font-size:12px; line-height:0; position:relative; Vertical-align:baseline; Bottom: -0.25em; " >1, S2, Sh }  be The indices of all the extreme Points, h≤n . Output the following integer as your solution:

1 * s2 * s3 *. * Sh * h) mod (n + 1)

{s1, S2, Sh }   for all poles,  h≤n , Then as your answer, output the following integer:

(S1 * s2 * s3 * ... * sh * h) mod (n + 1)

Sample input (enter sample)
107 9-8 -1-3 -11 4-3 96 -47 56 6-6 100 8

Sample output (export example)
7   // ( 9 x 2 x 6 x 7 x 1 x 5 ) % (10 + 1)

Limitation (limit)
    • 3 ≤ n  ≤10^5
    • all points on extreme edges is regarded as extreme points and hence should be include D in your solution.
    • time limit:2 sec
    • space limit:512 MB
    • 3 ≤ n ≤10^5
    • The coordinates of all points are integers within the range ( -10^5, 10^5), and there are no coincident points. Each point is randomly selected within ( -10^5, 10^5) x ( -10^5, 10^5) range
    • All points on the Poles are treated as poles, so they should not be omitted at the output.
    • Time Limit: 2 sec
    • Space limit: MB
Hint (hint)

CH algorithms presented in the lectures

Convex hull algorithm explained in the course

True TM after the twists and turns ah ... I can't find the pit.

Test instructions: The number of Poles of convex hull is obtained, then an expression is obtained.

The following: Andrew horizontal sequence scan, O (LOGN).

Pit point:

1. Note the collinear situation: simply change less than or equal to less than the reserved collinear condition.

    1. Cross (Ch[m-1]-ch[m-2], p[i]-ch[m-2]) <0
2. Data overflow problem: In addition to the last multiplication will overflow, the point of the coordinate cross product may overflow, all with a long long code:
#include <stdio.h> #include <algorithm>using namespace Std;const int max=100000+5;typedef long long ll;int N;    struct point{LL x, y;    int POS; Point (LL x=0,ll y=0,int p=0): X (x), Y (y), POS (p) {}} p[max],ch[max];typedef point Vector; Vector operator + (vector A, vector B) {return vector (a.x + b.x, A.Y + b.y);} Vector operator-(vector A, vector B) {return vector (a.x-b.x, a.y-b.y);} BOOL operator = = (CONST vector& A, const vector& B) {return a.x-b.x==0 && a.y-b.y==0;} LL Cross (vector A, vector B) {return a.x * b.y-a.y * b.x;}    BOOL CMP (point A, point B) {if (a.x! = b.x) return a.x < b.x; else return A.Y < b.y;}    int Convexhull () {sort (p+1,p+n+1,cmp);    int m = 0;        for (int i = 1; I <= n; i++) {when (M > 1 && Cross (Ch[m-1]-ch[m-2], p[i]-ch[m-2]) < 0) m--;    ch[m++] = P[i];    } if (m==n) return n;    int k = m; for (int i = n-1, I >=1; i--) {while (M > K &&cross (ch[m-1]-ch[m-2], P[i]-ch[m-2]) < 0) m--;    ch[m++] = P[i];    } if (n > 1) m--; return m;}    int main () {int m;            while (scanf ("%d", &n)!=eof) {for (int i = 1; I <= n; i++) {LL A, B;            scanf ("%lld%lld", &a,&b);        P[i] = point (A,b,i);        } m = Convexhull ();        printf ("%d\n", m);        LL Ans=1;        for (int i=0; i<m; i++) ans= ((ans% (n+1)) * (ch[i].pos% (n+1)))% (n+1);        Ans= ((ans% (n+1)) * (m% (n+1)))% (n+1);    printf ("%lld\n", ans); } return 0;}

Computational Geometry PA1 convex Hull (convex bag)

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.