长沙现场赛C题

硬币反弹问题解析

题目大意

给定两个同心圆,外面的圆表示的是一个区域,里面的圆是一个围墙,现在在圆形区域外某个点,以某个方向的速度发射一个硬币,使硬币做匀速直线运动,已知硬币撞上圆形围墙时会发生无能量损失的反弹,反弹的方式为镜面反射,硬币的任何部分在圆形区域内的时间。

解题思路

首先,对于我们所求的问题,求时间的话,只需要知道两个量,一个是硬币在圆形区域中经历的长度,另一个是速度,速度已经给定,而且明确说了没有能量损失,长度就需要我们自己求了。

次解(精度损失极大)

有一个非常直观的想法——首先,我们求出来速度所在的那个方向的直线与圆心的距离,这样就很容易能判断出来到底硬币有没有发生反弹,然后,对于反弹和没反弹分情况讨论即可。
对于没反弹的,很简单,丢出来硬币什么时候和外圆外切,然后与上述直线与圆心的距离做一个勾股定理,就能求出来长度,除以速度的模即可
对于反弹的,反弹前可以求出来它走了多远,仍然用勾股定理,我们求出来它碰到小圆的时候圆心所在的位置,然后就能想办法求出来反弹后速度的方向,经过旋转,然后再求出来另一部分即可。
——这个是高中生的做法……在程序设计里面,精度误差无法挽回,这也是我一直WA的原因……

次解改良版(精度损失仍然大但是可以通过卡精度来解决)

然后有一个改进,对于反弹后,我们大可不必那么麻烦,为啥呢?因为我们可以得知,反弹前和反弹后在圆形区域里面的长度实际上是完全对称的。由于碰撞点所在的位置是与圆形区域同圆心的圆上,那么碰撞点一定是它所在的大圆区域的割线一定是大圆区域的中点,在中点发生了镜面反射,那么一定是对称的(如下图)。
那么我们就可以得知,把前面求出来的东西乘以个2就可以了嘛……
但是实际上这个精度还是大问题,还是会被卡……

正解(精度损失小,可随心所欲)

于是乎就出现了一个神乎其神的算法(来自董适),把外面的大圆和里面的小圆的半径全都加上硬币半径r,那么就可以把硬币看做一个在其圆心上面的质点,问题瞬间就简单了……求出来速度所在的直线在大圆的割线和在小圆的割线,一减就是结果……
于是乎,现场赛里面,不会控制精度且智商不够的我就一直挂着这道题……

参考代码(ZOJ已AC)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
const double eps = 1e-9;
int dblcmp(double x){
    if (fabs(x) < eps) return 0;
    return x > 0 ? 1 : -1;
}
const double pi = acos(-1.0);
inline double sqr(double x){
    return x * x;
}
struct Point2D{
    double x, y;
    Point2D(){}
    Point2D(double a, double b) :
        x(a), y(b){}
    void input(){scanf("%lf%lf", &x, &y);}
    void output(){printf("%.3lf %.3lf\n", x, y);}
    Point2D operator + (const Point2D &a)const{return Point2D(a.x + x, a.y + y);}
    Point2D operator - (const Point2D &a)const{return Point2D(x - a.x, y - a.y);}
    Point2D operator * (const double &a)const{return Point2D(x * a, y * a);}
    Point2D operator / (const double &a)const{return Point2D(x / a, y / a);}
    bool operator == (const Point2D &a)const{return dblcmp(x - a.x) == 0 && dblcmp(y - a.y) == 0;}
    double len(){return hypot(x, y);}
};
double det(const Point2D &a, const Point2D &b){return a.x * b.y - a.y * b.x;}
double dot(const Point2D &a, const Point2D &b){return a.x * b.x + a.y * b.y;}
double dist(const Point2D &a, const Point2D &b){return (a - b).len();}
Point2D rotate(const Point2D &p, double a){
    double tx = p.x, ty = p.y;
    return Point2D(tx * cos(a) - ty * sin(a), tx * sin(a) + ty * cos(a));
}
typedef Point2D Vector2D;
struct Line2D{
    Point2D a, b;
    Line2D(){}
    Line2D(Point2D x, Point2D y) : 
        a(x), b(y){}
};
Point2D l2l_inst_p(Line2D a, Line2D b){
    double s1 = det(a.a - b.a, b.b - b.a);
    double s2 = det(a.b - b.a, b.b - b.a);
    Point2D res = (a.b * s1 - a.a * s2) / (s1 - s2);
    return res;
}
void l2c_inst_p(Point2D c, double r, Point2D l1, Point2D l2, Point2D &p1, Point2D &p2){
    Point2D p = c;
    double t;
    p.x += l1.y - l2.y;
    p.y += l2.x - l1.x;
    p = l2l_inst_p(Line2D(p, c), Line2D(l1, l2));
    t = sqrt(sqr(r) - sqr(dist(p, c))) / dist(l1, l2);
    p1.x = p.x + (l2.x - l1.x) * t;
    p1.y = p.y + (l2.y - l1.y) * t;
    p2.x = p.x - (l2.x - l1.x) * t;
    p2.y = p.y - (l2.y - l1.y) * t;
}
double p2l_dist(Point2D p, Point2D s, Point2D t){
    return fabs(det(s - p, t - p)) / dist(s, t);
}
Point2D ini; Vector2D v;
double r1, r2, r;
int main(){
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "rt", stdin);
    #endif
    while(~scanf("%lf", &r1)){
        scanf("%lf%lf", &r2, &r);
        ini.input(); v.input();
        if (dot(ini, v) >= 0){
            puts("0.000"); continue;
        }
        double vv = v.len();
        r1 += r; r2 += r;
        Point2D c = Point2D(0, 0);
        double mind = p2l_dist(c, ini, ini + v);
        if (dblcmp(mind - r2) >= 0){
            puts("0.000"); continue;
        }else if (dblcmp(mind - r1) >= 0){
            Point2D inter1, inter2;
            l2c_inst_p(c, r2, ini, ini + v, inter1, inter2);
            double l = dist(inter1, inter2);
            printf("%.3lf\n", l / vv);
        }else if (dblcmp(mind - r1) < 0){
            Point2D inter1, inter2, inter3, inter4;
            l2c_inst_p(c, r2, ini, ini + v, inter1, inter2);
            l2c_inst_p(c, r1, ini, ini + v, inter3, inter4);
            double l = dist(inter1, inter2), l1 = dist(inter3, inter4);
            double ans = (l - l1) / vv;
            printf("%.3f\n", ans);
        }
    }
    return 0;
}


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值