長沙現場賽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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章