Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

旋转卡壳~~

Posted by ecjtu_yuweiwei at 2014-08-06 21:18:14 on Problem 2187
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
#define LL long long
#define IT __int64
#define zero(x) fabs(x)<eps
#define mm(a,b) memset(a,b,sizeof(a))
const int INF=0x7fffffff;
const double inf=1e8;
const double eps=1e-10;
const double PI=acos(-1.0);
const int Max=50010;
using namespace std;
int sign(double x)
{
    return (x>eps)-(x<-eps);
}
typedef struct Node
{
    int x;
    int y;
    Node(const double &_x=0, const double &_y=0) : x(_x), y(_y) {}
    void input()
    {
        scanf("%d%d",&x,&y);
    }
    void output()
    {
        cout<<x<<" "<<y<<endl;
    }
} point;
point list[Max],stack[Max];
int n;
int top;


int cnt;
int curcnt;
int xmult(point p0,point p1,point p2)
{
    return(p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int dmult(point p0,point p1,point p2)
{
    return( (p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y));
}
int Distance(point p1,point p2)// 返回两点之间欧氏距离
{
    return((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
bool cmp(point p1,point p2)
{
    int temp;
    temp=xmult(list[0],p1,p2);
    if(temp>0)
        return true;
    if(temp==0&&(Distance(list[0],p1)<Distance(list[0],p2)))
        return true;
    return false;
}

void convex_hull()//凸包模板
{
    int i;
    for(i=1; i<n; i++)
    {
        point temp;
        if((list[i].y<list[0].y)||(list[i].y==list[0].y&&list[i].x<list[0].x))
            swap(list[0],list[i]);
    }
    sort(list+1,list+n,cmp);
    top=1;
    stack[0]=list[0];
    stack[1]=list[1];
    for(i=2; i<n; i++)
    {
        while(top>=1&&xmult(stack[top-1],stack[top],list[i])<=0)
            top--;
        top++;
        stack[top]=list[i];
    }
}
int rotating_calipers()//旋转卡壳
{
    int q=1,ans=0,p;
    stack[++top]=stack[0];
    for(p=0; p<top; p++)
    {
        while(xmult(stack[p],stack[p+1],stack[q+1])>xmult(stack[p],stack[p+1],stack[q]))
            q=(q+1)%top;
        ans=max(ans,max(Distance(stack[p],stack[q]),Distance(stack[p+1],stack[q+1])));
    }
    return ans;
}
int main()
{
    int m,i,j;
    int sum;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=0; i<n; i++)
        {
            list[i].input();
        }
        convex_hull();
        //cout<<"ok"<<endl;
        sum=0;
        /*for(i=0;i<=top;i++)
        {
            for(j=i+1;j<=top;j++)
            {
                sum=max(Distance(stack[i],stack[j]),sum);
            }
        }*/
        //cout<<top<<endl;
        sum=rotating_calipers();
        printf("%d\n",sum);
    }
    return 0;
}
/*
20
5521 -4185
157 3291
439 9868
4812 -2903
4993 5723
-9349 8008
-9267 -4272
-4861 -9500
6270 -4962
7394 -1099
-4349 8442
-1944 -491
5147 -5526
-2062 4624
-7775 8187
156 7746
-9646 -9686
1038 -2855
-9818 -4150
594 5175
答案:484066141
10
0 0
10000 0
1 100
2 199
9999 100
9998 199
100 -900
200 -1799
9800 -1799
9900 -900
答案:100000000
*/

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator