zoukankan      html  css  js  c++  java
  • poj2187 Beauty Contest

    Beauty Contest
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 39574   Accepted: 12275

    Description

    Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates. 

    Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms 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. Since Bessie refills her suitcase at every farm 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.Help Bessie by computing the maximum distance among all pairs of farms. 

    Input

    * Line 1: A single integer, N 

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

    Output

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

    Sample Input

    4
    0 0
    0 1
    1 1
    1 0
    

    Sample Output

    2
    

    Hint

    Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2) 

    Source

    题意:求两个点之间的最长距离的平方.
    分析:旋转卡壳的模板题.注意在最后求的时候会有从最后一个点到第一个点的循环.还要特判只有2个点的情况.叉积求面积要注意正负性
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int n,tot,ans;
    
    struct node
    {
        int x,y;
    }p[50010],q[50010];
    
    node sub(node a,node b)
    {
        node temp;
        temp.x = a.x - b.x;
        temp.y = a.y - b.y;
        return temp;
    }
    
    int det(node a,node b)
    {
        return a.x * b.y - a.y * b.x;
    }
    
    int dist(node a)
    {
        return (a.x - p[1].x) * (a.x - p[1].x) + (a.y - p[1].y) * (a.y - p[1].y);
    }
    
    bool cmp(node a,node b)
    {
        int temp = det(sub(a,p[1]),sub(b,p[1]));
        if (temp != 0)
            return temp > 0;
        return dist(a) < dist(b);
    }
    
    int nextt(int i)
    {
        if (i == tot)
            return 1;
        return i + 1;
    }
    
    void solve()
    {
        int id = 1;
        for (int i = 1; i <= n; i++)
            if (p[i].x < p[id].x || (p[i].x == p[id].x && p[i].y < p[id].y))
                id = i;
        if (id != 1)
            swap(p[id],p[1]);
        sort(p + 2,p + 1 + n,cmp);
        q[++tot] = p[1];
        for (int i = 2; i <= n; i++)
        {
            while (tot >= 2 && det(sub(p[i],q[tot - 1]),sub(q[tot],q[tot - 1])) >= 0)
                tot--;
            q[++tot] = p[i];
        }
    }
    
    int D(node a,node b)
    {
        return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
    }
    
    void solve2()
    {
        if (tot == 2)
        {
            ans = D(q[1],q[2]);
            return;
        }
        q[tot + 1] = q[1];
        for (int i = 1, j = 3; i <= tot; i++)
        {
            while (nextt(j) != i && det(sub(q[i + 1] , q[i]),sub(q[j],q[i])) <= det(sub(q[i + 1],q[i]),sub(q[nextt(j)],q[i])))
                j = nextt(j);
            ans = max(ans,D(q[j],q[i]));
            ans = max(ans,D(q[j],q[i + 1]));
        }
    }
    
    int main()
    {
        scanf("%d",&n);
        for (int i = 1; i <= n; i++)
            scanf("%d%d",&p[i].x,&p[i].y);
        solve();
        solve2();
        printf("%d
    ",ans);
    
        return 0;
    }
  • 相关阅读:
    【Java学习笔记之二十一】抽象类在Java继承中的用法小结
    【Java学习笔记之二十】final关键字在Java继承中的用法小结
    【Java学习笔记之十九】super在Java继承中的用法小结
    【Java学习笔记之十八】Javadoc注释的用法
    【Java学习笔记之十七】Java中普通代码块,构造代码块,静态代码块区别及代码示例分析
    hdu 5310(贪心)
    poj3268 最短路
    poj1797 最短路
    poj2253 最短路
    poj2387 最短路
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8111774.html
Copyright © 2011-2022 走看看