zoukankan      html  css  js  c++  java
  • 算法复习——凸包加旋转卡壳(poj2187)

    题目:

    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) 

    题解:

    凸包加旋转卡壳模板题,注意三个细节:凸包判断时注意变量i和tot,卡壳时注意一个next(j)!=i的条件,然后和只有两个点时的特判;

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<cctype>
    #include<cstring>
    #include<string>
    #include<ctime>
    #include<algorithm>
    using namespace std;
    struct point 
    {
      int x;
      int y;
    }p[50005],q[50005];
    inline int operator*(point a,point b)
    {
      return a.x*b.y-a.y*b.x;
    }
    inline point operator-(point a,point b)
    {
      point t;
      t.x=a.x-b.x;
      t.y=a.y-b.y;
      return t;
    }
    inline int norm(point a)
    {
      return a.x*a.x+a.y*a.y;
    }
    bool comp(int u,int v)
    {
      int det=(p[u]-p[1])*(p[v]-p[1]);
      if(det!=0)  return det>0;
      return norm(p[u]-p[1])<norm(p[v]-p[1]);
    }
    int n,m;
    int next(int i)
    {
      if(i!=m)  return ++i;
      else return 1;
    }
    void tubao()
    {
      int id=1;
      for(int i=2;i<=n;i++)
        if(p[id].x>p[i].x||(p[id].x==p[i].x&&p[id].y>p[i].y))
          id=i; 
      if(id!=1)  swap(p[id],p[1]); 
      int per[50005];
      for(int i=1;i<=n;i++)  per[i]=i;
      sort(per+2,per+n+1,comp);
      q[++m]=p[1];
      for(int i=2;i<=n;i++)
      {
        int j=per[i];
        while(m>=2&&(q[m]-q[m-1])*(p[j]-q[m-1])<=0)  m--;
        q[++m]=p[j];
      }
      q[++m]=p[1];
    }
    int area(point u,point v,point s)
    {
      return (u-s)*(v-s);
    }
    int solve()
    {
      if(m==2)  return (norm(q[1]-q[2]));
      q[m+1]=q[1];
      int res=0;
      for(int i=1,j=3;i<=m;i++) 
      {
        while(next(j)!=i&&area(q[i],q[i+1],q[j+1])>=area(q[i],q[i+1],q[j]))
          j=next(j);
        res=max(res,norm(q[i+1]-q[j]));
        res=max(res,norm(q[i]-q[j]));
      }
      return res;
    }
    int main()
    {
      //freopen("a.in","r",stdin);
      scanf("%d",&n);
      for(int i=1;i<=n;i++)
        scanf("%d%d",&p[i].x,&p[i].y);
      tubao();
      int ans=solve();
      cout<<ans<<endl;
    }
  • 相关阅读:
    我的第一个网络爬虫
    python中os模块的walk函数
    <Think Python>中统计文献单词的处理代码
    <Think Python>中斐波那契使用memo实现的章节
    Python中bisect的使用
    vim中使用系统粘贴板
    linux下的重命名
    HTML Dog 初级教程中关于 forms 的翻译
    unittest:1 用例编写
    python UI自动化实战记录十一: 总结
  • 原文地址:https://www.cnblogs.com/AseanA/p/6662407.html
Copyright © 2011-2022 走看看