zoukankan      html  css  js  c++  java
  • POJ

    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) 

    题意:给定二维平面上N个点,输出最远距离的平方。

    思路:先求出凸包,只考虑凸包上的点。利用旋转卡壳求最远距离。模板达成。

    #include<bits/stdc++.h>
    #define rep(i,a,b) for(int i=a;i<=b;i++)
    #define ll long long
    #define RC rotating_calipers
    using namespace std;
    const int maxn=100010;
    struct point{
        double x,y;
        point(double x=0,double y=0):x(x),y(y){}
        bool operator < (const point &c) const { return x<c.x||(x==c.x&&y<c.y);}
        point operator - (const point &c) const { return point(x-c.x,y-c.y);}
        double operator * (const point &c) const { return x*c.y-y*c.x; }
        double operator | (const point &c) const { return (x-c.x)*(x-c.x)+(y-c.y)*(y-c.y); }
    };
    double det(point A,point B){ return A.x*B.y-A.y*B.x;}
    double det(point O,point A,point B){ return det(A-O,B-O);}
    point a[maxn],ch[maxn];
    void convexhull(int n,int &top)
    {
        sort(a+1,a+n+1); top=0;
        for(int i=1;i<=n;i++){
            while(top>1&&det(ch[top-1],ch[top],a[i])<=0) top--;
            ch[++top]=a[i];
        }
        int ttop=top;
        for(int i=n-1;i>=1;i--){
            while(top>ttop&&det(ch[top-1],ch[top],a[i])<=0) top--;
            ch[++top]=a[i];
        }
    }
    double rotating_calipers(point p[],int top)
    {
        double ans=0; int now=2;
        rep(i,1,top-1){
            while(det(p[i],p[i+1],p[now])<det(p[i],p[i+1],p[now+1])){
                now++; //最远距离对应了最大面积。
                if(now==top) now=1;
            }
            ans=max(ans,(p[now]|p[i]));
        }
        return ans;
    }
    int main()
    {
        int N; scanf("%d",&N);
        for(int i=1;i<=N;i++)
          scanf("%lf%lf",&a[i].x,&a[i].y);
        int top; convexhull(N,top);
        printf("%lld",(ll)RC(ch,top));
        return 0;
    }
  • 相关阅读:
    377 TODOMVC:准备工作,配置 vue,列表渲染,添加任务,删除任务,编辑任务 ,Footer 的显示与隐藏
    376 vue指令:v-model (常用),v-text 和 v-html,v-bind (常用),操作样式,v-on,v-for,v-pre,v-once,v-cloak
    375 vue数据双向绑定演示:一个 input + v-model,Object.defineProperty,数据双向绑定的原理简单实现
    374 vue起步
    373 Vue 介绍,框架和库的区别 ,MVC + MVVM
    实现一个vue-router插件
    浅谈react context
    Flutter网络请求与JSON解析
    Vue中组件
    vue数据监听
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9619998.html
Copyright © 2011-2022 走看看