Ultraviolet A 11626-Convex Hull

Source: Internet
Author: User

Question: the points on the convex hull are output in a counterclockwise order, and the lower left corner is the starting point. Analysis: Computation of geometric and convex hull. The question will be given enough points on the convex bag, which can be ignored if they are not on the convex bag. Note: The handling of common points will overflow if int cross multiplication is used. [Cpp] # include <algorithm> # include <iostream> # include <cstdlib> # include <cstdio> # include <cmath> using namespace std; typedef struct pnode {double x, y, d;} point; point P [100001]; point S [100001]; point MP; double crossproduct (point a, point B, point c) // AB to ac {return (B. x-a.x) * (c. y-a.y)-(c. x-a.x) * (B. y-a.y);} double dist (point a, point B) {return sqrt (B. x-a.x) * (B. x-a.x) +. y-a.y )*( B. y-a.y);} bool cmp1 (point a, point B) {if (. x = B. x) return. y <B. y; else return. x <B. x;} bool cmp2 (point a, point B) {return crossproduct (P [0], a, B)> 0;} bool cmp3 (point a, point B) {double cp = crossproduct (P [0], a, B); if (cp = 0.0) {if (! Crossproduct (P [0], a, MP) // return. d> B. d; else return. d <B. d;} else return cp> 0;} void Graham (int N) {sort (P + 0, P + N, cmp1); sort (P + 1, P + N, cmp2); // process the same line for (int I = 1; I <N; ++ I) P [I]. d = dist (P [0], P [I]); MP = P [N-1]; sort (P + 1, P + N, cmp3 ); int top =-1; if (N> 0) S [++ top] = P [0]; if (N> 1) S [++ top] = P [1]; if (N> 2) {S [++ top] = P [2]; for (int I = 3; I <N; ++ I) {while (crossproduct (S [top-1], S [top], P [I]) <0) -- top; S [++ top] = P [I] ;}} printf ("% d \ n", top + 1); for (int I = 0; I <= top; ++ I) printf ("%. 0lf %. 0lf \ n ", S [I]. x, S [I]. y) ;}int main () {int N, T; char C; while (scanf ("% d", & T )! = EOF) while (T --) {scanf ("% d", & N); int count = 0; for (int I = 0; I <N; ++ I) {scanf ("% lf % c", & P [count]. x, & P [count]. y, & C); if (C = 'y') count ++;} Graham (count);} return 0 ;}

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.