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

为什么wa。。大牛们帮小弟看一下。。。谢了

Posted by zhangxizheng at 2012-05-31 16:23:33 on Problem 2187
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn=50005;

struct point{
    int x,y;
}P[maxn];

int multi(point a,point b,point c){
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
int dist(point a,point b){
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int cmp(const void*a,const void *b){
    point *c=(point*)a;
    point *d=(point*)b;
    int t=multi((*c),(*d),P[0]);
    if(t<=0)return 1;
    else if(t==0&&dist(*c,P[0])>dist(*d,P[0]))return 1;
    else return -1;
}

int roating_calipers(point que[],int N){
    int q=1,ret=0;
    que[N]=que[0];
    for(int i=0;i<N;++i){
        while(multi(que[(i+1)%N],que[(q+1)%N],que[i])>multi(que[(i+1)%N],que[q],que[i])) q=(q+1)%N;
        ret=max(ret,max(dist(que[i],que[q]),dist(que[(i+1)%N],que[(q+1)%N])));
    }
    return ret;
}

int Graham(point que[],int &len,int n){
    int mini=0;
    for(int i=1;i<n;++i){
        if(P[i].y<P[mini].y||(P[i].y==P[mini].y&&P[i].x<P[mini].x)) mini=i;
    }
    if(mini!=0) swap(P[mini],P[0]);
    qsort(P,n,sizeof(P[0]),cmp);
    que[0]=P[0];
    que[1]=P[1];
    que[2]=P[2];
    int top=2;
    for(int i=3;i<n;++i){
        while(top>0&&multi(que[top],P[i],que[top-1])<0) top--;
        que[++top]=P[i];
    }
    len=top+1;
    int s=roating_calipers(que,len);
    return s;
}
int main(){
//    freopen("in.txt","r",stdin);
    int n;
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;++i){
            scanf("%d %d",&P[i].x,&P[i].y);
        }
        if(n==2) printf("%d\n",dist(P[0],P[1]));
        else{
            point que[maxn];
            int len;
            int R=Graham(que,len,n);
            printf("%d\n",R);
        }
    }
    return 0;
}

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