Alyona and Triangles CodeForces - 682E 旋转卡壳找最大三角形

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/leekerian/article/details/82154010
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;


const int maxn=11111;
const double eps=1e-8;

int sgn(double x)
{
    if(fabs(x)<eps)
        return 0;
    if(x<0)
        return -1;
    else
        return 1;
}
struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x=_x;
        y=_y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x-b.x,y-b.y);
    }
    Point operator +(const Point &b)const
    {
        return Point(x+b.x,y+b.y);
    }
    double operator ^(const Point &b)const
    {
        return x*b.y-y*b.x;
    }
    double operator *(const Point &b)const
    {
        return x*b.x+y*b.y;
    }
};

Point p[maxn];
int n;
int Stack[maxn];
int top;


double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b));
}
bool cmp(Point a,Point b)
{
    int ans=sgn((a-p[0])^(b-p[0]));
    if(ans==1)
        return true;
    else if(ans==0)
        return dist(a,p[0])<dist(b,p[0]);
    else
        return false;
}
void graham()
{
    for(int i=0;i<n;i++)
    {
        if(p[i].x<p[0].x||(p[i].x==p[0].x&&p[i].y<p[0].y))
            swap(p[i],p[0]);
    }
    sort(p+1,p+n,cmp);
    Stack[0]=0;
    Stack[1]=1;
    top=2;
    for(int i=2;i<n;i++)
    {
        while(top>1&&sgn((p[Stack[top-1]]-p[Stack[top-2]])^(p[i]-p[Stack[top-2]]))<=0)
            top--;
        Stack[top++]=i;
    }
}
Point p1[maxn];
Point p_1,p_2,p_3;

void r_a()
{
    int l=1;
    int l1=1;
    double res=0;
    for(int i=0;i<top;i++)
    {
        while(sgn(((p1[l1]-p1[i])^(p1[l]-p1[i]))-((p1[l1]-p1[i])^(p1[(l+1)%top]-p1[i])))<0)
            l=(l+1)%top;
        while(sgn(((p1[l1]-p1[i])^(p1[l]-p1[i]))-((p1[(l1+1)%top]-p1[i])^(p1[l]-p1[i])))<0)
            l1=(l1+1)%top;
        if(sgn(((p1[l1]-p1[i])^(p1[l]-p1[i]))-res)>=0)
        {
            res=((p1[l1]-p1[i])^(p1[l]-p1[i]));
            p_1=p1[i];
            p_2=p1[l1];
            p_3=p1[l];
        }
    }
    //cout<<res<<endl;
}
int main()
{
    long long s;
    scanf("%d %lld",&n,&s);
    for(int i=0;i<n;i++)
    {
        scanf("%lf %lf",&p[i].x,&p[i].y);
    }
    graham();
    for(int i=0;i<top;i++)
    {
        p1[i]=p[Stack[i]];
        //cout<<p1[i].x<<" "<<p1[i].y<<endl;
    }
    r_a();
    //cout<<p_1.x<<" "<<p_1.y<<endl;
    //cout<<p_2.x<<" "<<p_2.y<<endl;
    //cout<<p_3.x<<" "<<p_3.y<<endl;
    Point ans1;
    Point ans2;
    Point ans3;
    ans1=p_3-p_1+p_2;
    ans2=p_1-p_3+p_2;
    ans3=p_3-p_2+p_1;
    printf("%.0lf %.0lf\n",ans1.x,ans1.y);
    printf("%.0lf %.0lf\n",ans2.x,ans2.y);
    printf("%.0lf %.0lf\n",ans3.x,ans3.y);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/leekerian/article/details/82154010