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

这样也CE 我就想不同了!

Posted by haliluya at 2010-07-28 21:40:46 on Problem 2187
#include<iostream>
#include<cmath>
#include <cstdio>
#include<algorithm>
#include <string.h>
#include <string>
using namespace std;
#define size 50008

int stack[size], cnt, n, l;

struct point
{
    int x, y;
}a[size];

bool cmp(point p1, point p2)
{
    int temp = (p1.x - a[0].x) * (p2.y - a[0].y) - (p2.x - a[0].x) * (p1.y - a[0].y);
    if (temp == 0) 
		return p1.x < p2.x;
    else 
		return temp > 0;
}

bool CrossLeft(point p1, point p2, point p3) 
{
    return ((p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y)) < 0;
}

int distance(point p1,point p2)
{
	return (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y);
}


void Graham()
{
    sort(a + 1, a + n, cmp);
	cnt = 0;
    stack[cnt++] = 0;
	stack[cnt++] = 1;
    for(int i = 2; i < n; i++)
    {
        while(cnt > 1 && !CrossLeft(a[stack[cnt - 2]], a[stack[cnt - 1]], a[i])) 
			cnt--;
        stack[cnt++] = i;
    }
    stack[cnt] = 0;
}


int main()
{
	int tx, ty, p,i;
    while(scanf("%d", &n) != EOF )
	{
		tx = ty = 1000000;
		for(i = 0; i < n; i++)
		{
			scanf("%d%d", &a[i].x, &a[i].y);
			if (a[i].y < ty || (a[i].y == ty && a[i].x < tx))
			{
				tx = a[i].x, ty = a[i].y;
				p = i;
			}
		}
		point temp = a[p];
		a[p] = a[0];
		a[0] = temp;
		Graham();
   		int k;
		int max = -1;
		stack[cnt + 1] = 1;
		stack[cnt + 2] = 2;
		int temp2 ,ans = -1;
		for(i=0,k=1;i<cnt ;i++)
		{
			for(k=i+1 ;k<cnt ;k++)
			{
				temp2 = distance(a[stack[i]],a[stack[k]]);
				if(temp2 > ans)
					ans = temp2;
			}
		}
		printf("%d\n",ans);
		memset(stack,0,sizeof(stack));
		
	}
    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