CodeForces - 908C New Year and Curling(圆与圆之间的关系的问题)

Carol is currently curling.

She has n disks each with radius r on the 2D plane.

Initially she has all these disks above the line y = 10100.

She then will slide the disks towards the line y = 0 one by one in order from 1 to n.

When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and xcoordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk.

Compute the y-coordinates of centers of all the disks after all disks have been pushed.

Input

The first line will contain two integers n and r (1 ≤ n, r ≤ 1 000), the number of disks, and the radius of the disks, respectively.

The next line will contain n integers x1, x2, ..., xn (1 ≤ xi ≤ 1 000) — the x-coordinates of the disks.

Output

Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6.

Namely, let's assume that your answer for a particular value of a coordinate is aand the answer of the jury is b. The checker program will consider your answer correct if  for all coordinates.

Example
Input
6 2
5 5 6 8 3 12
Output
2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613
Note

The final positions of the disks will look as follows:

In particular, note the position of the last disk.


题意: 已知一些圆在 (xi,10100) 处,然后依次落下,下落时只要碰到某个圆或 达到y=0处,就会自动停止,就算再被其他球碰住也不会动,求这些圆的最后的y值,也即是水平高度


说实话考试时,这道题我没做出来,wrong anwer  7;

做了这道题给我的收获是:1:以后再判断圆与圆的位置关系时,就用两个圆心与半径之间的关系来判断;2:因为这个是依次放球,他可能会和当前坐标左边的碰撞也也能和右边的碰撞,怎么判断它与那个球碰撞?

我开始的思路就是找它前面的球能与它相撞,且圆心位置最高的球;但是 wrong anwer  7;说实话,我现在还不确定这样判断为啥错,希望看了我博客的同学们,找到我的错误,给我留言,告诉我那错了,谢谢!qq邮箱:755672829@qq.com

收获2:我们可以换一种思路,就是这个球与其他某个球相撞了,这个球现在位置一定是当前能达到最高的高度,因为这个球是从高处y  = 10^100下滑的,碰撞住东西就停住;

错误代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 1010
#include<math.h>

struct node
{
	double t;
	double l,r;
	double h;
}stu[Max];
int n;
double r;

void fff(int j,int i)
{
	double kk = sqrt((2*r*2*r)-(stu[i].t-stu[j].t)*(stu[i].t-stu[j].t));
	stu[i].h = stu[j].h + kk;
}
int main()
{
	int i,j;
	while(~scanf("%d%lf",&n,&r))
	{
		for(i= 0 ;i < n;i++)
		{
			scanf("%lf",&stu[i].t);
			stu[i].l = stu[i].t - r;
			stu[i].r = stu[i].t + r;
			double hh = -1;
			int flag = -1;
			for(j = 0;j < i;j ++)
			{
				if(stu[i].l>stu[j].r||stu[j].l>stu[i].r)
					continue;
				if(stu[j].h>hh)
				{
					flag = j;
					hh = stu[j].h;
				}		
			}
			if(flag == -1)
				stu[i].h = r;
			else
				fff(flag,i);		
		}
		for(i=0;i<n;i++)
		{
			if(i==n-1) printf("%.10f\n",stu[i].h);
			else printf("%.10f ",stu[i].h);
		}
	}
	return 0;
}
正确的代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 1010
#include<math.h>

struct node
{
	double t;
	double h;
}stu[Max];
int n;
double r;
int main()
{
	int i,j;
	while(~scanf("%d%lf",&n,&r))
	{
		for(i= 0 ;i < n;i++)
		{
			scanf("%lf",&stu[i].t);
			stu[i].h = -1;
			for(j = 0;j < i;j ++)
			{
				// 能落到的一定是最大高度;
				if(fabs(stu[i].t-stu[j].t)<=2*r) 
					stu[i].h = max(stu[i].h,stu[j].h+(sqrt((2*r*2*r)-(stu[i].t-stu[j].t)*(stu[i].t-stu[j].t))));
			}
			if(stu[i].h==-1)
				stu[i].h = r;		
		}
		for(i=0;i<n;i++)
		{
			if(i==n-1) printf("%.10f\n",stu[i].h);
			else printf("%.10f ",stu[i].h);
		}
	}
	return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值