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