C. New Year and Curling

这里写图片描述

题意:

已知一些圆在(xi,10100)(xi,10100)处,然后依次落下,下落时只要碰到某个圆,就会停止,求这些圆的最后的y值,也即是水平高度
参考大神的
这里写图片描述

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

typedef long long LL;
const int maxn = 1100;
double x[maxn],y[maxn];

int main()
{
    int n;
    double r;
    scanf("%d %lf", &n, &r);
    for(int i = 0; i < n; i++) {
        scanf("%lf", &x[i]);
    }
    y[0]=r;
    for(int i=1;i<n;i++)
    {
        double res = r;
        for(int j=i-1;j>=0;j--)
        {
            double ans = fabs(x[i]-x[j]);
            if(ans <= r+r) /*Á½Ô²ÏàÇÐʱ*/
                res = max(res, y[j] + sqrt(4.0 * r * r - ans * ans));
        }
        y[i] = res;
    }
    for(int i=0;i<n;i++)
    {
        if(i) printf(" ");
        printf("%.15lf", y[i]);
    }
    printf("\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/q451792269/article/details/80767462