codeforces#320(div2) C A Problem about Polyline 二分

codeforces#320(div2) C  A Problem about Polyline  二分

C. A Problem about Polyline
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a polyline going through points (0, 0) – (x, x) – (2x, 0) – (3x, x) – (4x, 0) – ... - (2kx, 0) – (2kx + x, x) – ....

We know that the polyline passes through the point (a, b). Find minimum positive value x such that it is true or determine that there is no such x.

Input

Only one line containing two positive integers a and b (1 ≤ a, b ≤ 109).

Output

Output the only line containing the answer. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9. If there is no such x then output  - 1 as the answer.

Sample test(s)
input
3 1
output
1.000000000000
input
1 3
output
-1
input
4 1
output
1.250000000000
Note

You can see following graphs for sample 1 and sample 3.

3

 

显然,x=(a+b)/(2*k)>=b

二分解不等式解出k就行了

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll INF=(1LL<<60);
const double EPS=0.00000000000001;

ll a,b;

ll bin(ll l,ll r)
{
    ll res=-1;
    while(l<=r){
        ll m=(l+r)>>1;
        double x=(a+b)*1.0/(2*m);
        if(x>=b) l=m+1,res=m;
        else r=m-1;
        //cout<<l<<" "<<r<<" "<<m<<endl;
        //cout<<x<<" "<<b<<endl;
        //printf("%.1f d\n",x,b);
    }
    return res;
}

int main()
{
    while(cin>>a>>b){
        if(a<b){
            puts("-1");
            continue;
        }
        ll k=bin(1,INF);
        if(k==-1){
            puts("-1");continue;
        }
        printf("%.10f\n",(a+b)*1.0/(2*k));
    }
    return 0;
}
View Code

 

posted @ 2015-09-17 02:45  __560  阅读(368)  评论(0编辑  收藏  举报