F: Traveling

F.  Traveling

Time Limit: 3000MS

Description

Alice is a Beautiful horse,earning the title 'Miss Horse World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) grassland around the world in order to spread goodwill to the other horses

. For simplicity, the world will be represented as a two-dimensional plane, where each grassland  is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two grasslands share the same pair of coordinates.

 

Even thoughAlicetravels directly in a straight line between pairs of grasslands, the distance between some grasslands can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. SinceAlicerefills her suitcase at every grassland she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring.HelpAliceby computing the maximum distance among all pairs of grasslands.

Input

* Line 1: A single integer, N

* Lines 2...N+1: Two space-separated integers x and y specifying coordinate of each grassland

Output

* Line 1: A single integer that is the squared distance between the pair of grasslands that are farthest apart from each other.

 Sample Input

Sample Output

4

0 0

0 1

1 1

1 0

2

 

 

 

 

 

 

题意:FJ有n块田地,每块田地在图上可以看为是1个点,并已知这n个点的坐标,问其中最远的两点距离的平方为多少。

 

 

AC代码:

#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
struct Point
{
    int x, y;
   
};
Point pts[50100];
Point pcs[50100];//凸包上的点
int nN;
int nM;
bool cmp( Point a,Point b)
{
   return a.y<b.y||(a.y==b.y&&a.x<b.x);
}
inline int SDis(const Point& a, const Point& b)
{
    return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}

int  Det(double fX1, double fY1, double fX2, double fY2)
{
    return fX1 * fY2 >fX2 * fY1;
}

int  Cross(Point a, Point b, Point c)
{
    return Det(b.x - a.x, b.y - a.y, c.x - a.x, c.y - a.y);
}

void Convex()
{
    sort(pts, pts + nN,cmp);//先排y坐标,坐标相同的再按x坐标排序
 
    nM = 0;
    for (int i = 0; i < nN; ++i)//正向
    {
        while(nM >= 2 && !Cross(pcs[nM - 2], pcs[nM - 1], pts[i]))
        {
            nM--;
        }
        pcs[nM++] = pts[i];
    }
  int   t = nM + 1;
    for (int i= nN - 2 ; i >= 0; --i)//反向
    {
        while (nM >= t && !Cross(pcs[nM - 2], pcs[nM - 1], pts[i]) )
        {
            nM--;
        }
        pcs[nM++] = pts[i];
    }
    nM--;//起点会被重复包含
}

int main()
{
    while (scanf("%d", &nN) == 1)
    {
        for (int i = 0; i < nN; ++i)
        {
            scanf("%d%d", &pts[i].x, &pts[i].y);
        }
        Convex();
        int nMax = -1;
      
      
        for (int i = 0; i < nM; ++i)
        {
            for (int j = i + 1; j < nM; ++j)
            {
                nMax = max(nMax, SDis(pcs[i], pcs[j]));
            }
        }
        printf("%d\n", nMax);
    }
   
    return 0;
}

posted @ 2012-08-05 17:15  jiai  Views(204)  Comments(0Edit  收藏  举报