Codeforces 579 C. A Problem about Polyline(Codeforces Round #320 (Div. 2) )

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.


题目大意:
就是给你一个坐标(a,b),看是否在一个锯齿形的折线上,如果有输出最小的x,
否则输出-1;
解题思路:
就是画一下图,然后根据点在折线上,(斜率只有1和-1),设比较的斜率
为a/b,然后当k是奇数的时候要加1;然后ans =  (a+b)/k;
上代码:

/**
2015 - 09 - 22 晚上

Author: ITAK

Motto:

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
**/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 1000;
const int mod = 1e9+7;
const double eps = 1e-7;
struct node
{
    int x, y, node;
} arr[maxn*maxn];

bool cmp(node a, node b)
{
    return a.node > b.node;
}

int main()
{
    int a, b;
    cin>>a>>b;
    int k = a/b;
    if(a < b)
        puts("-1");
    else
    {
        if(k%2 == 1)
            k++;
        double ans = 1.0*(a+b)/(double)k;
        printf("%.10lf\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值