Triangle POJ - 2079 (旋转卡壳最大三角形)

六眼飞鱼酱① 提交于 2019-11-28 22:59:53
Given n distinct points on a plane, your task is to find the triangle that have the maximum area, whose vertices are from the given points.

Input

The input consists of several test cases. The first line of each test case contains an integer n, indicating the number of points on the plane. Each of the following n lines contains two integer xi and yi, indicating the ith points. The last line of the input is an integer −1, indicating the end of input, which should not be processed. You may assume that 1 <= n <= 50000 and −10 4 <= xi, yi <= 10 4 for all i = 1 . . . n.

Output

For each test case, print a line containing the maximum area, which contains two digits after the decimal point. You may assume that there is always an answer which is greater than zero.

Sample Input

3
3 4
2 6
2 7
5
2 6
3 9
2 0
8 0
6 5
-1

Sample Output

0.50
27.00SOLUTION:板子:CODE:
#include <iostream>
#include <cstdio>
#include"cmath"
#include"iomanip"
#include <algorithm>
using namespace std;
const int N=5e4+10;
struct point{
    int x,y;
}p[N];
int dis(point a,point b){
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int cross(point p0,point p1,point p2){
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int cmp1(point p1,point p2){
    return p1.x<p2.x||(p1.x==p2.x&&p1.y<p2.y);
}
int cmp2(point p1,point p2){


    int c=cross(p[0],p1,p2);
    if(c==0) return p1.x<p2.x;
    return c>0;
}
point sta[N];
int top;
void convex(int n){
    top=0;
    sort(p,p+n,cmp1);
    sort(p+1,p+n,cmp2);
    sta[top++]=p[0];
    sta[top++]=p[1];

    for(int i=2;i<n;){

        if(top>1&&cross(sta[top-2],p[i],sta[top-1])>=0)top--;
        else {
            sta[top++]=p[i++];
        }

    }
}

int rotating()
{
    int q=1;
    int ans=0;
   // cout<<"all:"<<top<<endl;
    sta[top]=sta[0];
    int j,k;
    j=1,k=2;

    for(int i=0;i<top;i++)
    {
        while(cross(sta[i],sta[j],sta[(k+1)%top])>cross(sta[i],sta[j],sta[k]))
            k=(k+1)%top;
        while(cross(sta[i],sta[(j+1)%top],sta[(k)%top])>cross(sta[i],sta[j],sta[k]))
            j=(j+1)%top;

        ans=max(ans,abs(cross(sta[i],sta[j],sta[k])));
    }
    cout<<fixed<<setprecision(2)  <<0.5*ans<<endl;
  /*  double t=13.444;
    cout<<fixed<<setprecision(0)<<t<<endl;
    cout<<t<<endl;
    cout<<fixed<<setprecision(2);
    cout<<t<<endl;*/
}

int main()
{
    //freopen("cin.txt","r",stdin);
    int n;
    while(cin>>n&&n!=-1){
        for(int i=0;i<n;i++){
            scanf("%d%d",&p[i].x,&p[i].y);
        }
        convex(n);
        rotating();
      //  printf("%d\n",rotating());
    }
    return 0;
}

  


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!