zoukankan      html  css  js  c++  java
  • POJ2187(旋转卡壳)

    Beauty Contest

    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 35459   Accepted: 10978

    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) 
     
    利用旋转卡壳求最远点对的距离平方。
     1 //2016.10.2
     2 #include <iostream>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <cmath>
     6 #include <algorithm>
     7 #define N 50005
     8 #define eps 1e-8
     9 
    10 using namespace std;
    11 
    12 int n;
    13 
    14 struct point
    15 {
    16     double x, y;
    17     point(){}
    18     point(double a, double b):x(a), y(b){}
    19     point operator-(point a){//向量减法
    20         return point(x-a.x, y-a.y);
    21     }
    22     double operator*(point a){//向量叉积
    23         return x*a.y-y*a.x;
    24     }
    25     bool operator<(const point a)const{
    26         if(fabs(x-a.x)<eps)return y<a.y;//浮点数的判等不能直接用‘==’直接比较
    27         return x<a.x;
    28     }
    29     double len2(){//向量模的平方
    30         return x*x+y*y;
    31     }
    32 }p[N];
    33 
    34 struct polygon
    35 {
    36     int n;
    37     point p[N];
    38 }pg;
    39 
    40 double cp(point o, point a, point b)//向量oa,ob叉积
    41 {
    42     return (a-o)*(b-o);
    43 }
    44 
    45 void Convex(int &n)//Graham扫描法
    46 {
    47     sort(p, p+n);
    48     int top, m;
    49     pg.p[0] = p[0]; pg.p[1] = p[1]; top = 1;
    50     for(int i = 2; i < n; i++)//从前往后扫
    51     {
    52         while(top>0 && cp(p[i], pg.p[top], pg.p[top-1])>=0)top--;
    53         pg.p[++top] = p[i];
    54     }
    55     m = top;
    56     pg.p[++top] = p[n-2];
    57     for(int i = n-3; i >= 0; i--)//从后往前扫
    58     {
    59         while(top>m && cp(p[i], pg.p[top], pg.p[top-1])>=0)top--;
    60         pg.p[++top] = p[i];
    61     }
    62     pg.n = top;
    63 }
    64 
    65 int rotating_calipers()//旋转卡壳
    66 {
    67     int v = 1;n = pg.n;
    68     double ans = 0;
    69     pg.p[n] = pg.p[0];
    70     for(int u = 0; u < n; u++)//旋转
    71     {
    72         while(cp(pg.p[u],pg.p[u+1],pg.p[v+1])>cp(pg.p[u],pg.p[u+1],pg.p[v]))v = (v+1)%n;
    73         ans = max(ans, max((pg.p[u]-pg.p[v]).len2(), (pg.p[u+1]-pg.p[v+1]).len2()));
    74     }
    75     return ans;
    76 }
    77 
    78 int main()
    79 {
    80     int n;
    81     while(scanf("%d", &n)!=EOF && n)
    82     {
    83         for(int i = 0; i < n; i++)
    84               scanf("%lf%lf", &p[i].x, &p[i].y);
    85         Convex(n);
    86         int ans = rotating_calipers();
    87         printf("%d
    ", ans);
    88     }
    89 
    90     return 0;
    91 }
  • 相关阅读:
    Go jaegerde 应用【logger+gorm+grpc+http】
    Go gRPC 调试工具
    iris和xxl-job整合
    Go Grpc部署到 k8s【端口共享】
    rocketmq事务 go 采用rocketmq-client-go的实现
    Go Grpc部署到 k8s【负载均衡】
    ubuntu18安装Kubernetes 1.20.5
    k8s Python API
    go nacos服务发现
    k8s集群日志收集ELK和graylog
  • 原文地址:https://www.cnblogs.com/Penn000/p/5927741.html
Copyright © 2011-2022 走看看