Poj 2187 beauty contest (convex hull + rotating jamming case)

Source: Internet
Author: User

Reprint please indicate the source, thank you http://blog.csdn.net/ACM_cxlove? Viewmode = Contents
By --- cxlove

Question: Find the maximum diameter of the convex hull.

Http://poj.org/problem? Id = 2187

First, evaluate the convex hull of a polygon.

Then, the maximum diameter is obtained by rotating the jamming case.

In fact, two parallel lines are clamped out of the convex hull and rotated.

If Pa and Pb are the farthest points, then the parallel line of PA and PB must be rotated, and there must be a side that overlaps with the polygon.

This is used to enumerate each edge and find the farthest point from the edge. It looks like n ^ 2, just like enumeration.

However, because it is a convex polygon, for a certain edge, the distance from the point to the edge shows a single-peak function.

Then we can use the farthest point of the last edge to move it back.

Well said this: http://www.cnblogs.com/xdruid/archive/2012/07/01/2572303.html

# Include <iostream> # include <fstream> # include <iomanip> # include <cstdio> # include <cstring> # include <algorithm> # include <cstdlib> # include <cmath> # include <set> # include <map> # include <queue> # include <stack> # include <string> # include <vector> # include <sstream> # include <ctime> # include <cassert> # define ll long # define EPS 1e-8 # define INF 999999.0 # define zero () ABS (a) <EPS # define N 20 # define PI ACOs (-1.0) using namespa Ce STD; struct point {int X, Y;} p [50005]; int N; vector <point> S; int dist (point P1, point P2) {return (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y);} int xmul (point P0, Point P1, point P2) {return (p1.x-release X) * (p2.y-release y)-(p2.x-release X) * (p1.y-release y);} // graham_scan evaluate the convex hull bool CMP (point P1, point P2) {If (xmul (P [0], P1, P2)> EPS) return true; else if (zero (xmul (P [0], P2, P1 )) & dist (P [0], P1) <dist (P [0], P2) return true; RET Urn false;} void graham_scan () {for (INT I = 1; I <n; I ++) if (P [I]. Y <p [0]. Y | (P [I]. y = P [0]. Y & P [I]. x <p [0]. x) Swap (P [I], p [0]); sort (p + 1, P + N, CMP); S. clear (); S. push_back (P [0]); S. push_back (P [1]); For (INT I = 2; I <n; I ++) {While (S. size ()> = 2 & xmul (s [S. size ()-2], s [S. size ()-1], p [I]) <EPS) s. pop_back (); S. push_back (P [I]) ;}// calculates the convex hull diameter void rotating_calipers () {int ans = 0, Len = S. size (); Int J = 1; S. push_back (s [0]); (Int I = 0; I <Len; I ++) {// find the farthest point from the straight line while (ABS (xmul (s [I], s [I + 1], s [J + 1])> ABS (xmul (s [I], s [I + 1], s [J]) J = (J + 1) % Len; ans = max (ANS, max (Dist (s [I + 1], s [J]), dist (s [I], s [J]);} printf ("% d \ n", ANS);} int main () {While (scanf ("% d", & N )! = EOF) {for (INT I = 0; I <n; I ++) scanf ("% d", & P [I]. x, & P [I]. y); graham_scan (); rotating_calipers ();} 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.