[hdu] 6097 "优秀的黄金分割三分"

题解写了这句话,比赛时写了三分。T 到飞起。赛后发现三分写丑了。然后,还是需要快速读入。果然我还是菜呀、

#include"bits/stdc++.h"
using namespace std;
const double Pi = acos(-1.0);
double x1, y1, x2, y2;
double rr;
typedef long long LL;
struct FastIO
{
    static const int S = 2 * 100;
    int wpos;
    char wbuf[S];
    FastIO() : wpos(0) {}
    inline int xchar()
    {
        static char buf[S];
        static int len = 0, pos = 0;
        if (pos == len)
            pos = 0, len = fread(buf, 1, S, stdin);
        if (pos == len) exit(0);
        return buf[pos ++];
    }
    inline int xuint()
    {
        int c = xchar(), x = 0;
        while (c <= 32) c = xchar();
        for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
        return x;
    }
    inline int xint()
    {
        int s = 1, c = xchar(), x = 0;
        while (c <= 32) c = xchar();
        if (c == '-') s = -1, c = xchar();
        for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
        return x * s;
    }
    inline void xstring(char *s)
    {
        int c = xchar();
        while (c <= 32) c = xchar();
        for (; c > 32; c = xchar()) * s++ = c;
        *s = 0;
    }
    inline void wchar(int x)
    {
        if (wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0;
        wbuf[wpos ++] = x;
    }
    inline void wint(LL x)
    {
        if (x < 0) wchar('-'), x = -x;
        char s[24];
        int n = 0;
        while (x || !n) s[n ++] = '0' + x % 10, x /= 10;
        while (n--) wchar(s[n]);
    }
    inline void wstring(const char *s)
    {
        while (*s) wchar(*s++);
    }
    ~FastIO()
    {
        if (wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0;
    }
} io;
double get_lens(double arg1)
{
    double x11 = rr * cos(arg1);
    double y12 = rr * sin(arg1);
    return sqrt((x11 - x1) * (x11 - x1) + (y12 - y1) * (y12 - y1)) + sqrt((x11 - x2) * (x11 - x2) + (y12 - y2) * (y12 - y2));
}
int main()
{
    //freopen("data.in", "r", stdin);
    //freopen("data1.out", "w", stdout);
    int T;
    double d = (sqrt(5) - 1) / 2;
    T = io.xint();
    while(T --){
        rr = io.xint();
        x1 = io.xint(); y1 = io.xint();
        x2 = io.xint(); y2 = io.xint();
        double t = x1 * x1 + y1 * y1;
        double a = atan2(y1, x1);
        double b = atan2(y2, x2);
        if(a < 0) a += 2 * Pi;
        if(b < 0) b += 2 * Pi;
        double l, r;
        if(a > b){
            r = a; l = b;
        }
        else{
            r = b; l = a;
        }
        if(r - l > Pi){
            r -= 2 * Pi;
            swap(l , r);
        }
        r = (l + r) / 2.0;
        double midl = (l * d + r * (1 - d));
        double midr = (l * (1 - d) + r * d);
        double lans = get_lens(midl);
        double rans = get_lens(midr);
        for(int i = 1; i <= 30; i ++){
            if(lans < rans){
                r = midr;
                midr = midl;
                midl = l * d + r * (1 - d);
                rans = lans;
                lans = get_lens(midl);
            }
            else{
                l = midl;
                midl = midr;
                midr = l * (1 - d) + r * d;
                lans = rans;
                rans = get_lens(midr);
            }
        }
        printf("%.12f\n", get_lens(l));
    }
    return 0;
}


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭