2015年6月8日 星期一

Looksery Cup 2015, problem: (H) Degenerate Matrix

code好久qaq

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <set>
#include <string>
#include <cstring>
#include <map>
#include <cmath>
#include <utility>
#include <limits>

using namespace std;

//  a-p b-q   <= t
//  c-r d-s
//  a-t <= p <= t+a

const long double inf = 1e27;

struct pr{long double l, r; pr(long double _l = 0, long double _r = 0):l(_l), r(_r){return;}};

pr operator*(const pr& p1, const pr& p2){
    long double par[4] = {p1.l*p2.l, p1.r*p2.r, p1.l*p2.r, p1.r*p2.l};
    pr ret(inf, -inf);
    ret.l = min(ret.l, par[0]), ret.r = max(ret.r, par[0]);
    ret.l = min(ret.l, par[1]), ret.r = max(ret.r, par[1]);
    ret.l = min(ret.l, par[2]), ret.r = max(ret.r, par[2]);
    ret.l = min(ret.l, par[3]), ret.r = max(ret.r, par[3]);
    return ret;
}

bool check(pr p1, pr p2){
    if(p1.l > p2.l) swap(p1, p2);
    return p1.r >= p2.l;
}

int getint(){ int r; scanf("%d", &r); return r;}

int main(){
    long double a, b, c, d;
    a = getint(); b = getint(); c = getint(); d = getint();
    long double tmin = 0, tmax = max(max(max(fabs(a), fabs(b)), fabs(c)), fabs(d));
    while(tmax-tmin >= 1e-9){
        long double tmid = (tmax+tmin)/(long double)2;
        //printf("[%.9f %.9f %.9f]\n", (double)tmin, (double)tmax, (double)tmid);
        pr rg1 = pr(a-tmid, a+tmid)*pr(d-tmid, d+tmid);
        pr rg2 = pr(b-tmid, b+tmid)*pr(c-tmid, c+tmid);
        if(check(rg1, rg2))
            tmax = tmid;
        else
            tmin = tmid;
    }
    printf("%.9lf\n", (double)tmax);
    return 0;
}

沒有留言:

張貼留言