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;
}


目录
相关文章
Codeforces Round #192 (Div. 2) (330B) B.Road Construction
要将N个城市全部相连,刚开始以为是最小生成树的问题,其实就是一道简单的题目。 要求两个城市之间不超过两条道路,那么所有的城市应该是连在一个点上的,至于这个点就很好找了,只要找到一个没有和其他点有道路限制的即可。
40 0
【CodeForces】Codeforces Round 857 (Div. 2) B
【CodeForces】Codeforces Round 857 (Div. 2) B
129 0
|
机器学习/深度学习 Java
codeforces Educational Codeforces Round 49 (Rated for Div. 2) C题
刚开始拿到这题很懵逼,知道了别人的思路之后开始写,但是还是遇到很多坑,要求求P2/S最大。p=a b。就是求(a2+ b2 +2ab)/ab最大,也就是a/b +b/a最大。那么题意就很明显了。
115 0
Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861A k-rounding【暴力】
A. k-rounding time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output ...
1244 0