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;
    }
  • 相关阅读:
    对技术的态度
    码农提高工作效率
    为什么要使用String
    Java还是程序员的金饭碗
    为什么Java7开始在数字中使用下划线
    阿里云快速搭建一个静态网站
    IDEA中Springboot项目部署到阿里云linux服务器
    Linux环境下安装宝塔面板
    阿里云服务器的购买和配置以及搭建项目教程
    linux服务器上部署springboot项目,并让他持续运行到后台
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8111774.html
Copyright © 2011-2022 走看看