Codeforces 50C Happy Farm 5 Convex bag

Source: Internet
Author: User

Topic Link: Click to open the link

= = Rare Y out of a computational geometry.

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include < iostream>using namespace std; #define INF 999999999.9#define PI acos ( -1.0) #define LL Long longstruct point{ll x, y, dis; }PT[400005], stack[400005], p0;ll top, tot;//calculate geometry distance ll Dis (ll X1, ll Y1, ll x2, ll y2) {return (X1-X2) * (X1-X2) + (y1-y2) * (y1-y 2);} Polar comparison, return -1:P0P1 on the right side of P0P2, return 0:p0,p1,p2 collinear int Cmp_polarangel (struct point p1, struct point p2, struct point pb) {double de Lta= (p1.x-pb.x) * (P2.Y-PB.Y)-(p2.x-pb.x) * (P1.Y-PB.Y), if (delta<0.0) return 1;else if (delta==0.0) return 0;else return-1;}  Determines whether the vector p2p3 is p1p2 to the left-handed bool Is_leftturn (struct point P3, struct-point P2, struct-point p1) {int Type=cmp_polarangel (P3, p1, p2); if (type<0) return True;return false;} First, by the angle row, and then by the distance from the small to the big row int Cmp (const VOID*P1, const VOID*P2) {struct point*a1= (struct point*) p1;struct point*a2= (struct point *) P2;int Type=cmp_polarangel (*A1, *A2, p0); if (type<0) Return-1;else if (type==0) {if (A1-&GT;DIS&LT;A2-&GT;dis) Return-1;else if (A1->dis==a2->dis) return 0;else return 1;} else return 1;} The convex package ll step[4][2] = {0,1,0,-1,1,0,-1,0};void Solve (ll N) {ll I, K;p0.x=p0.y=inf;ll x,y;for (i=0;i<n;i++) {scanf ("%I 64d%i64d ", &x, &y); for (ll j = 0; J < 4; J + +) {Point P = {x+step[j][0], y+step[j][1]};p t[i*4+j] = p;if (Pt[i*4+j] . y < P0.y) {p0.y=pt[i*4+j].y;p0.x=pt[i*4+j].x;k=i*4+j;} else if (PT[I*4+J].Y==P0.Y) {if (pt[i*4+j].x<p0.x) {p0.x=pt[i*4+j].x;k=i*4+j;}}}} n *= 4;pt[k]=pt[0];p t[0]=p0;for (i=1;i<n;i++) Pt[i].dis=dis (PT[I].X,PT[I].Y, P0.X,P0.Y); Qsort (pt+1, n-1, sizeof ( Tot=1;for (i=2;i<n;i++) if (Cmp_polarangel (Pt[i], pt[i-1], p0)), and/or minus the polar angle of the same point. pt[tot++]=pt[i-1];p t[ Tot++]=pt[n-1];top=1;stack[0]=pt[0];stack[1]=pt[1];for (i=2;i<tot;i++) {while (Top>=1 && Is_LeftTurn ( Pt[i], Stack[top], stack[top-1]) ==false) top--;stack[++top]=pt[i];}} inline ll Abs (ll x) {return x>0?x:-x;} ll Len (Point A, point B) {return abs (a.x-b.x) + ABS (A.Y-B.Y)-min (ABS (a.x-b.x), ABS (A.Y-B.Y));} int main () {ll n, x, Y;while (cin>>n) {Solve (n); ll ans = 0;for (ll i = 0; i < top; i++) ans + = Len (stack[i), stack[i +1]); ans + = Len (Stack[top], stack[0]); Cout<<ans<<endl;} 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.