zoukankan      html  css  js  c++  java
  • 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
    ",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
    ",(a+b)*1.0/(2*k));
        }
        return 0;
    }
    View Code
    没有AC不了的题,只有不努力的ACMER!
  • 相关阅读:
    设置函数环境——setfenv(转)
    全局变量声明的规范化(转)
    利用__index和__newindex实现默认值表、监控表、只读表(转)
    php中的$_GET怎样获取带有井号“#”的參数
    Servlet配置load-on-startup
    LinQ—扩展方法
    CRT
    [C++] 获取IE代理server的账号password
    一步一步写算法(之hash表)
    android之PackageManager简单介绍
  • 原文地址:https://www.cnblogs.com/--560/p/4815088.html
Copyright © 2011-2022 走看看