欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CodeForces - 908C (暴力枚举)

程序员文章站 2022-06-09 17:56:20
...

C. New Year and Curling

time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Carol is currently curling.

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

Initially she has all these disks above the liney = 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 x coordinate 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 a and 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:
CodeForces - 908C (暴力枚举)
In particular, note the position of the last disk.

问题分析

题意:从上面往下落球,如果球达到y坐标为0时或者碰到其他球都会停止。给出球的半径和每个球的横坐标,求最后每个球的圆心Y坐标。

每当下落一个球,就去判断它会和哪个球碰到,枚举可以和它发生碰撞的球的最高y坐标加上高度即可(h = sqrt(4*r*r-x*x)勾股定理)。
CodeForces - 908C (暴力枚举)
好,下面是AC code(^_^)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
using namespace std;
const int N = 1500;
int v[N];
double ans[N];
int main()
{
    // freopen("in.txt","r",stdin);
    int n,r;
    cin>>n>>r;
    for(int i = 0; i < n; i++)
    cin>>v[i];
    int t = (r+r)*(r+r);
    for(int i = 0; i < n; i++)
    {
        int x;double temp = r;
        for(int j = 0; j < i; j++)
        {
            x = fabs(v[i]-v[j]);
            if(x<=2*r)//碰撞条件
            {
                temp = max(ans[j]+sqrt(fabs(t-x*x)),temp);
            }
        }
        ans[i] = temp;
        printf("%.10f ",ans[i]);
    }
    return 0;
}