UVA 4728 squares (convex hull + rotating jam)

Source: Internet
Author: User

Title Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17267

Ideas

Convex hull + rotating jam

The convex hull is obtained, and the diameter of the convex hull can be calculated by rotating the jam.

Code

1#include <cstdio>2#include <vector>3#include <iostream>4#include <algorithm>5 using namespacestd;6 7 structPt {8     intx, y;9Pt (intx=0,inty=0): X (x), Y (y) {};Ten }; One typedef Pt VEC; A  -Vecoperator-(Pt a,pt B) {returnVEC (a.x-b.x,a.y-b.y); } - BOOL operator< (Constpt& A,Constpt&b) { the     returna.x<b.x | | (a.x==b.x && a.y<b.y); - } - BOOL operator== (Constpt& A,Constpt&b) { -     returna.x==b.x && a.y==b.y; + } -  + intDot (VEC A,vec B) {returna.x*b.x+a.y+b.y;} A intCross (VEC A,vec B) {returna.x*b.y-a.y*b.x;} at intDist (Pt a,pt B) { -     return(a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (a.y-b.y); - } -  - intN; -Vector<pt> Convexhull (vector<pt>p) { in sort (P.begin (), P.end ()); - p.erase (Unique (P.begin (), P.end ()), P.end ()); to     intn=p.size (); +     intm=0; -Vector<pt> CH (n+1); the      for(intI=0; i<n;i++) { *          while(m>1&& Cross (ch[m-1]-ch[m-2],p[i]-ch[m-2]) <=0) m--; $ch[m++]=P[i];Panax Notoginseng     } -     intk=m; the      for(inti=n-2; i>=0; i--) { +          while(M>k && Cross (ch[m-1]-ch[m-2],p[i]-ch[m-2]) <=0) m--; Ach[m++]=P[i]; the     } +     if(n>1) m--; - ch.resize (m); $     returnch; $ } -  - intDiameter (vector<pt> PS) {//Rotating Jam theVector<pt> p=Convexhull (PS); -     intn=p.size ();Wuyi     if(n==1)return 0;//Special Case Processing the     if(n==2)returnDist (p[0],p[1]); -P.push_back (p[0]);//For u Wu     intans=0; -      for(intu=0, v=1; u<n;u++) {                 About          for(;;) {             $             intDiff=cross (p[u+1]-p[u],p[v+1]-p[v]); -             if(diff<=0) {//at this point the UV is the heel point -ans=Max (Ans,dist (p[u],p[v)); -                 if(!diff) Ans=max (Ans,dist (p[u],p[v+1]));//Parallel A                  Break; +             } thev= (v+1)%N; -         } $     } the     returnans; the } the voidReadint&x) { the     CharC=GetChar (); -      while(!isdigit (c)) c=GetChar (); inx=0; the      while(IsDigit (c)) thex=x*Ten+c-'0', c=GetChar (); About }  the intMain () { the     intT; the read (T); +      while(t--) { -         intN; Read (n); theVector<pt>PS;Bayi          for(intI=0; i<n;i++) { the             intx,y,w; the Read (x), read ( y), read (w); - Ps.push_back (Pt (x, y)); -Ps.push_back (Pt (x+w,y)); thePs.push_back (Pt (x,y+W)); thePs.push_back (Pt (x+w,y+W)); the         } theprintf"%d\n", Diameter (PS)); -     } the     return 0; the}

UVA 4728 squares (convex hull + rotating jam)

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.