zoukankan      html  css  js  c++  java
  • POJ-1981 Circle and Points 单位圆覆盖

      题目链接:http://poj.org/problem?id=1981

      容易想到直接枚举两个点,然后确定一个圆来枚举,算法复杂度O(n^3).

      这题还有O(n^2*lg n)的算法。将每个点扩展为单位圆,依次枚举每个单位圆,枚举剩下的单位圆,如果有交点,每个圆产生两个交点,然后对产生的2n个交点极角排序,判断被覆盖最多的弧,被覆盖相当于这个弧上的点为圆心的圆可以覆盖到覆盖它的那些点,所以被覆盖最多的弧就是答案了。

    O(n^3):

      1 //STATUS:C++_AC_4032MS_208KB
      2 #include <functional>
      3 #include <algorithm>
      4 #include <iostream>
      5 //#include <ext/rope>
      6 #include <fstream>
      7 #include <sstream>
      8 #include <iomanip>
      9 #include <numeric>
     10 #include <cstring>
     11 #include <cassert>
     12 #include <cstdio>
     13 #include <string>
     14 #include <vector>
     15 #include <bitset>
     16 #include <queue>
     17 #include <stack>
     18 #include <cmath>
     19 #include <ctime>
     20 #include <list>
     21 #include <set>
     22 #include <map>
     23 using namespace std;
     24 //using namespace __gnu_cxx;
     25 //define
     26 #define pii pair<int,int>
     27 #define mem(a,b) memset(a,b,sizeof(a))
     28 #define lson l,mid,rt<<1
     29 #define rson mid+1,r,rt<<1|1
     30 #define PI acos(-1.0)
     31 //typedef
     32 typedef long long LL;
     33 typedef unsigned long long ULL;
     34 //const
     35 const int N=310;
     36 const int INF=0x3f3f3f3f;
     37 const int MOD=100000,STA=8000010;
     38 const LL LNF=1LL<<60;
     39 const double EPS=1e-8;
     40 const double OO=1e15;
     41 const int dx[4]={-1,0,1,0};
     42 const int dy[4]={0,1,0,-1};
     43 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
     44 //Daily Use ...
     45 inline int sign(double x){return (x>EPS)-(x<-EPS);}
     46 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
     47 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
     48 template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
     49 template<class T> inline T Min(T a,T b){return a<b?a:b;}
     50 template<class T> inline T Max(T a,T b){return a>b?a:b;}
     51 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
     52 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
     53 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
     54 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
     55 //End
     56 
     57 struct Node{
     58     double x,y;
     59 }nod[N],O;
     60 int n;
     61 
     62 double dist(Node &a,Node &b)
     63 {
     64     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
     65 }
     66 
     67 void getO(Node &a,Node &b,int dir)
     68 {
     69     double t=dist(a,b)/2.0;
     70     t=dir*sqrt((1.0-t*t));
     71     if(a.y==b.y){
     72         O.x=(a.x+b.x)/2.0;
     73         O.y=a.y+t;
     74     }
     75     else if(a.x==b.x){
     76         O.y=(a.y+b.y)/2.0;
     77         O.x=a.x+t;
     78     }
     79     else {
     80         double kt;
     81         kt=atan(-(a.x-b.x)/(a.y-b.y));
     82         O.x=(a.x+b.x)/2.0+cos(kt)*t;
     83         O.y=(a.y+b.y)/2.0+sin(kt)*t;
     84     }
     85 }
     86 
     87 int main()
     88 {
     89  //   freopen("in.txt","r",stdin);
     90     int i,j,k,ans,tot;
     91     while(scanf("%d",&n) && n)
     92     {
     93         ans=1;
     94         for(i=0;i<n;i++){
     95             scanf("%lf%lf",&nod[i].x,&nod[i].y);
     96         }
     97         for(i=0;i<n;i++){
     98             for(j=i+1;j<n;j++){
     99                 if(dist(nod[i],nod[j])<2.0){
    100                     getO(nod[i],nod[j],1);
    101                     for(tot=2,k=0;k<n;k++){
    102                         if(k==i || k==j)continue;
    103                         if(dist(O,nod[k])-1.0<EPS)tot++;
    104                     }
    105                     if(tot>ans)ans=tot;
    106                 }
    107             }
    108         }
    109 
    110         printf("%d
    ",ans);
    111     }
    112     return 0;
    113 }    

    O(n^2*lg n): 建立极角的时候,不是以枚举的圆心 i->j 方向的向量,而是 j->i 方向的向量,因为 i->j 方向不能完全判断圆的方向,在极角排序的时候会出错。

      1 //STATUS:C++_AC_750MS_212KB
      2 #include <functional>
      3 #include <algorithm>
      4 #include <iostream>
      5 //#include <ext/rope>
      6 #include <fstream>
      7 #include <sstream>
      8 #include <iomanip>
      9 #include <numeric>
     10 #include <cstring>
     11 #include <cassert>
     12 #include <cstdio>
     13 #include <string>
     14 #include <vector>
     15 #include <bitset>
     16 #include <queue>
     17 #include <stack>
     18 #include <cmath>
     19 #include <ctime>
     20 #include <list>
     21 #include <set>
     22 #include <map>
     23 using namespace std;
     24 //using namespace __gnu_cxx;
     25 //define
     26 #define pii pair<int,int>
     27 #define mem(a,b) memset(a,b,sizeof(a))
     28 #define lson l,mid,rt<<1
     29 #define rson mid+1,r,rt<<1|1
     30 #define PI acos(-1.0)
     31 //typedef
     32 typedef long long LL;
     33 typedef unsigned long long ULL;
     34 //const
     35 const int N=310;
     36 const int INF=0x3f3f3f3f;
     37 const int MOD=100000,STA=8000010;
     38 const LL LNF=1LL<<60;
     39 const double EPS=1e-8;
     40 const double OO=1e15;
     41 const int dx[4]={-1,0,1,0};
     42 const int dy[4]={0,1,0,-1};
     43 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
     44 //Daily Use ...
     45 inline int sign(double x){return (x>EPS)-(x<-EPS);}
     46 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
     47 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
     48 template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
     49 template<class T> inline T Min(T a,T b){return a<b?a:b;}
     50 template<class T> inline T Max(T a,T b){return a>b?a:b;}
     51 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
     52 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
     53 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
     54 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
     55 //End
     56 
     57 struct Node{
     58     double x,y;
     59 }nod[N];
     60 struct Point{
     61     double angle;
     62     int id;
     63     bool operator < (const Point& a)const{
     64         return angle!=a.angle?angle<a.angle:id>a.id;
     65     }
     66 }p[N*2];
     67 int n;
     68 
     69 double dist(Node &a,Node &b)
     70 {
     71     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
     72 }
     73 
     74 int slove()
     75 {
     76     int i,j,ans,tot,k,cnt;
     77     ans=1;
     78     for(i=0;i<n;i++){
     79         for(j=k=0;j<n;j++){
     80             if(j==i || dist(nod[i],nod[j])>2.0)continue;
     81             double angle=atan2(nod[i].y-nod[j].y,nod[i].x-nod[j].x);  //注意为i-j的向量方向
     82             double phi=acos(dist(nod[i],nod[j])/2);
     83             p[k].angle=angle-phi;p[k++].id=1;
     84             p[k].angle=angle+phi;p[k++].id=-1;
     85         }
     86         sort(p,p+k);
     87         for(tot=1,j=0;j<k;j++){
     88             tot+=p[j].id;
     89             ans=Max(ans,tot);
     90         }
     91     }
     92     return ans;
     93 }
     94 
     95 int main()
     96 {
     97  //   freopen("in.txt","r",stdin);
     98     int i;
     99     while(~scanf("%d",&n) && n)
    100     {
    101         for(i=0;i<n;i++)
    102             scanf("%lf%lf",&nod[i].x,&nod[i].y);
    103 
    104         printf("%d
    ",slove());
    105     }
    106     return 0;
    107 }
  • 相关阅读:
    Vue2.5 旅游项目实例27 联调测试上线-项目打包上线
    Vue2.5 旅游项目实例26 联调测试上线-真机测试
    Vue2.5 旅游项目实例25 联调测试上线-项目前后端联调
    Vue2.5 旅游项目实例24 详情页-在项目中添加基础动画
    Vue2.5 旅游项目实例23 详情页 Ajax动态获取数据
    Vue2.5 旅游项目实例22 详情页 使用递归组件实现详情页列表
    HTML5标签embed详解
    MongoDB使用经验总结
    16个非常酷的jQuery插件
    kendo-ui的MVVM模式
  • 原文地址:https://www.cnblogs.com/zhsl/p/3192144.html
Copyright © 2011-2022 走看看