POJ 2079 Triangle convex bag + rotating jam

Source: Internet
Author: User

Test instructions

For n points on a plane, the maximum triangular area of the n points is obtained.

Analysis:

Rotating jam, but to pay attention to the difference between the most distant point of the plane, the largest triangle of the edge is not necessarily on the convex hull, but also posted in the previously written to find the plane the furthest point of the POJ 2187 code as a comparison.

Code:

POJ 2079//sep9#include <iostream> #include <algorithm> using namespace std;    const int maxn=50012;      struct P {int x, y;  }PNT[MAXN],CNT[MAXN];    int n;      int cmp (P a,p b) {if (A.Y!=B.Y) return a.y<b.y;  Return a.x<b.x;      } int Cross (P a,p b,p c) {int x1=b.x-a.x;      int y1=b.y-a.y;      int x2=c.x-a.x;      int y2=c.y-a.y;  return x1*y2-x2*y1;      } int Graham () {sort (pnt,pnt+n,cmp);      int I,pos=1;      CNT[0]=PNT[0];      CNT[1]=PNT[1];          for (I=2;i<n;++i) {while (Pos>0&&cross (Cnt[pos-1],cnt[pos],pnt[i]) <=0)--pos;            Cnt[++pos]=pnt[i];      } int bak=pos;          for (I=n-2;i>=0;--i) {while (Pos>bak&&cross (Cnt[pos-1],cnt[pos],pnt[i]) <=0)--pos;      Cnt[++pos]=pnt[i];  } return POS; } int main () {while (scanf ("%d", &n) ==1&&n!=-1) {for (int i=0;i<n;++i) scanf ("%d%d", &pnt[i]. x,&pnt[i].y);              int Pos=graham (); int Ans=0,p=1,i,j,k;for (i=j=k=0;i<pos;++i) {while (ABS (Cross (cnt[i],cnt[j],cnt[(k+1)%pos)) >abs (Cross (cnt[i ],CNT[J],CNT[K])) k= (k+1)%pos;while (ABS (Cross (cnt[i],cnt[(j+1)%pos],cnt[k)) >abs (Cross (CNT[I],CNT[J],CNT[K)        )) j= (j+1)%pos;ans=max (Ans,abs (Cross (cnt[i],cnt[j],cnt[k)));}      printf ("%.2lf\n", ans/2.0);}     return 0;    }

The code that asks for the farthest point pair:

POJ 2187//sep9#include<iostream> #include <algorithm>using namespace std;struct point{int x, y;} Points[50012],bag[50012];int Cross (Point p1,point p2,point p0) {return (p1.x-p0.x) * (P2.Y-P0.Y)-(p2.x-p0.x) * ( P1.Y-P0.Y);} int CMP (point A,point B) {return a.y==b.y?a.x<b.x:a.y<b.y;} int dis (point a,point b) {return (a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y);}    int Getbag (point p[],int n,point bag[]) {int i,bp=1; Bag[0]=p[0];bag[1]=p[1];for (I=2;i<n;++i) {while (Bp&&cross (BAG[BP-1],P[I],BAG[BP]) <=0)--bp;bag[++ Bp]=p[i];} int Len=bp;bag[++bp]=p[n-2];for (i=n-3;i>=0;--i) {while (Bp>len&&cross (BAG[BP-1],P[I],BAG[BP]) <=0 )--bp;bag[++bp]=p[i];} return BP;} int main () {int p,i,n;while (scanf ("%d", &n) ==1) {for (i=0;i<n;++i) scanf ("%d%d", &points[i].x,&points[i ].Y), sort (points,points+n,cmp), int dif,ans=0,m=getbag (points,n,bag);p =1;for (i=0;i<m;++i) {while (ABS (Cross (bag [i],bag[i+1],bag[p+1]) >abs (Cross (bag[i],bag[i+1],bag[p))) p= (p+1)%m;ans=max (Ans,max (Dis (bag[I],BAG[P]), DIS (bag[i+1],bag[p+1]));} printf ("%d\n", ans);}}



POJ 2079 Triangle convex bag + 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.