CCPC秦皇岛 ZOJ3993 Safest Buildings(半径比较/圆的相交面积)

Safest Buildings

Time Limit: 1 Second       Memory Limit: 65536 KB

PUBG is a multiplayer online battle royale video game. In the game, up to one hundred players parachute onto an island and scavenge for weapons and equipment to kill others while avoiding getting killed themselves. BaoBao is a big fan of the game, but this time he is having some trouble selecting the safest building.

There are  buildings scattering on the island in the game, and we consider these buildings as points on a two-dimensional plane. At the beginning of each round, a circular safe area whose center is located at (0, 0) with radius  will be spawned on the island. After some time, the safe area will shrink down towards a random circle with radius  (). The whole new safe area is entirely contained in the original safe area (may be tangent to the original safe area), and the center of the new safe area is uniformly chosen within the original safe area.

The buildings covered by the new safe area is called the safe buildings. Given the radius of the safe areas and the positions of the buildings, BaoBao wants to find all the buildings with the largest probability to become safe buildings.

Input

There are multiple test cases. The first line of input contains an integer , indicating the number of test cases. For each test case:

The first line contains three integers  (),  and  (), indicating the number of buildings and the radius of two safe circles.

The following  lines each contains 2 integers  and  (), indicating the coordinate of the buildings. Here we assume that the center of the original safe circle is located at , and all the buildings are inside the original circle.

It's guaranteed that the sum of  over all test cases will not exceed 5000.

Output

For each test case output two lines.

The first line contains an integer , indicating the number of buildings with the highest probability to become safe buildings.

The second line contains  integers separated by a space in ascending order, indicating the indices of safest buildings.

Please, DO NOT output extra spaces at the end of each line.

Sample Input
2
3 10 5
3 4
3 5
3 6
3 10 4
-7 -6
4 5
5 4
Sample Output
1
1
2
2 3

秦皇岛站的最后一题,因为题意比较迷,当时在现场百思不得其解。题意是小的毒物圈会落在大的里面,问最安全的建筑。

圆心一定有一个可行域,是以圆点为圆心,R-r为半径的圆,然后就要看各个建筑被覆盖的概率是多大,也就是以建筑为圆心,r为半径的圆与前面提到的可行域圆的相交面积。

取前几个相交面积最大且相等的就是答案。

当时现场用比较半径写的,回来改了改就过了,后来用求相交面积的函数,这样就很简单了。


比较相交面积代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
#define mem(a,b) memset(a,b,sizeof(a))
const int maxn = 1e5+5;
const int ff = 0x3f3f3f3f;
const double esp = 1e-7;

struct Circle
{
	double x,y,r,s;
	int pos;
} c[maxn];

double qarea(Circle c1,Circle c2)
{
	double dis = sqrt((c1.x - c2.x) * (c1.x - c2.x) + (c1.y - c2.y) * (c1.y - c2.y));  
    if(c1.r + c2.r <= dis) return 0;//两圆相离  
    if(c1.r - c2.r >= dis)//两园相含 c1包含c2  
        return acos(-1.0) * c2.r * c2.r;  
    if(c2.r - c1.r >= dis)//两园相含 c2包含c1  
        return acos(-1.0) * c1.r * c1.r;  
    //两圆相交  
    double angle1 = acos((c1.r * c1.r + dis * dis - c2.r * c2.r) / (2 * dis * c1.r));  
    double angle2 = acos((c2.r * c2.r + dis * dis - c1.r * c1.r) / (2 * dis * c2.r));  
    return c1.r * c1.r * angle1 + c2.r * c2.r * angle2 - sin(angle1) * c1.r * dis; 
}

bool cmp(Circle &a,Circle &b)
{
	return a.s> b.s;
}

int n,r1,r2;
int ans[maxn];

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>r1>>r2;
		Circle st;
		st.x = st.y = 0;
		st.r = r1-r2;//小毒圈圆心可行域半径
		for(int i = 1;i<= n;i++)
		{
			scanf("%lf %lf",&c[i].x,&c[i].y);
			c[i].pos = i;
			c[i].r = r2;
			c[i].s = qarea(c[i],st);
		}
		
		sort(c+1,c+n+1,cmp);//按照相交面积排序
		int cnt = 1;
		ans[1] = c[1].pos;
		for(int i = 2;i<= n;i++)
		{
			if(fabs(c[i].s - c[1].s)> esp)
				break;
			ans[++cnt] = c[i].pos;
		}
		sort(ans+1,ans+cnt+1);//对答案排序
		
		printf("%d\n",cnt);
		for(int i = 1;i< cnt;i++)
			printf("%d ",ans[i]);
		printf("%d\n",ans[cnt]);
	}
	return 0;
}


比较半径代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
#define mem(a,b) memset(a,b,sizeof(a))
const int maxn = 1e5+5;
const int ff = 0x3f3f3f3f;
const double esp = 1e-7;

int n;
int r1,r2;
double a[maxn];
int ans[maxn];

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n;
		cin>>r1>>r2;
		for(int i  = 1;i<= n;i++)
		{
			int x,y;
			scanf("%d %d",&x,&y);
			a[i] = sqrt((double)(x*x)+(double)(y*y));
		}
		
		int len = 0;
		int flag = 0;
		double maxd = -ff;
		int r = r1-r2;//小毒圈圆心可行域半径 
		for(int i = 1;i<= n;i++)
		{
			if(!flag)
			{
				if(fabs(r2 - a[i] - maxd)< esp)
					ans[++len] = i;
				else if(r2 - a[i]> maxd)
				{
					len = 0;
					ans[++len] = i;
					maxd = r2 - a[i];
				}
				if(r2>= r&&r2 - a[i]>= r)//当r2>= r时,以建筑为圆心的圆能把小毒圈圆心可行域圆包围 
				{
					flag = 1;
					maxd = r;
				}
				else if(r2< r&&r2+a[i]<= r)//当r2< r时,小毒圈圆心可行域圆能把以建筑为圆心的圆包围 
				{
					flag = 2;
					maxd = r;
				}//以上两种情况是分别当r2>= r和r2< r的最大相交面积 
			}
			else 
			{
				if(flag == 1&&r2 - a[i]>= maxd)
					ans[++len] = i;
				else if(flag == 2&&r2+a[i]<= maxd)
					ans[++len] = i;
			}
		}
		printf("%d\n",len); 
		for(int i = 1;i< len;i++)
			printf("%d ",ans[i]);
		printf("%d\n",ans[len]);
	}	
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值