UPC D: Gone Fishing(三角函数)

题目描述
It is getting dark and the mosquitoes are attacking Fisherman Floyd. Floyd decides to throw his circular net one last time and wants to make the most out of the last throw.

Given the size (radius) of Floyd’s net and positions (x,y) of a set of fish, what is the maximum fish Floyd can catch with one throw? That is, find the maximum points you can have in the net (circle).
If a point (fish) is within 10 -6 of the circle boundary, consider the point in the circle.
输入
The first input line provides the radius of the circle. The second input line contains an integer, n (1 ≤ n ≤ 100), indicating the number of fish. Each of the next n input lines provides the location (x,y) for one fish. Assume all these points are distinct. Also assume that all the x and y values in
the input (as well as the circle radius) are integers between 1 and 1000, inclusive.
输出
Print the maximum fish Floyd can catch with one throw.
样例输入 Copy
【样例1】
20
4
6 7
5 5
190 100
4 4
【样例2】
3
2
1 1
95 4
样例输出 Copy
【样例1】
3
【样例2】
1

题目大意:
在二维平面中给出 n 个点,再给出一个固定大小的圆,问如何放置这个圆可以使其覆盖最多的点

题目分析:
首先不难想到一种 n^3 的做法,就是两层循环去枚举两个点,因为两个不同的点就可以确定下来两个圆了(对称的),然后对于这 2 * n * n 个圆的每一个来说,再用一层循环去枚举 n 个点,计算一下有多少个点可以被覆盖到就可以了。

还有一种差分+相交弧的n^2logn的做法,不会,但放个链接:链接

下面是自己n^3的做法,比上面的那个博客要思路清晰。

#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int maxn=100+7;
const double eps=1e-6;
double m;
struct point{
    
    
    double x,y;
    point(){
    
    }
    point(double _x,double _y):x(_x),y(_y){
    
    }
}g[maxn];
bool cmp(point P,point Q){
    
      return P.x<Q.x||(P.x==Q.x&&P.y<Q.y);  }
double SqrDis(point P,point Q){
    
     return (P.x-Q.x)*(P.x-Q.x)+(P.y-Q.y)*(P.y-Q.y); }
int i,j,ans,n;
int solve(point P,point Q){
    
    
    point tmp=point((P.x+Q.x)/2,(P.y+Q.y)/2);
    double angle=atan2(P.y-Q.y,P.x-Q.x)+acos(-1.0)/2;	//关键角度计算
    double len=sqrt(m*m-SqrDis(P,Q)/4);
    tmp.x+=cos(angle)*len;
    tmp.y+=sin(angle)*len;
    int i,ret=0;
    for(i=0;i<n;++i) if(SqrDis(g[i],tmp)<=m*m+eps)    ++ret;
    return ret;
}
int main(){
    
    
    scanf("%lf%d",&m,&n);
    for(i=0;i<n;++i) scanf("%lf%lf",&g[i].x,&g[i].y);
    sort(g,g+n,cmp);
    ans=1;
    for(i=0;i<n;++i)
        for(j=i+1;j<n&&(g[j].x-g[i].x)<=m*2;++j)
            if(SqrDis(g[i],g[j])<=4*m*m)
                ans=max(ans,max(solve(g[i],g[j]),solve(g[j],g[i])));
    printf("%d",ans);
}

猜你喜欢

转载自blog.csdn.net/weixin_45606191/article/details/109059337
UPC