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

题目:
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.

Examples
inputCopy
3 1
outputCopy
1.000000000000
inputCopy
1 3
outputCopy
-1
inputCopy
4 1
outputCopy
1.250000000000

可知答案肯定是在直线a=b之下的,因此b>a时输出-1。
设x=b,可知点(a,b)肯定是在两个顶峰之间,并且x从b变化到2b的过程中肯定会经过此点,在这个范围内二分即可。
用叉积判断点和直线的位置关系(AP*AB叉积为正,AP在AB顺时针方向)

/* ***********************************************
Author        :ACagain
Created Time  :2018/4/16 15:14:12
File Name     :4_16.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;

#define lson o<<1,l,m
#define rson o<<1|1,m+1,r
#define pii pair<int,int>
#define mp make_pair
#define ll long long
#define INF 0x3f3f3f3f
const double eps=0.000000001;
struct p
{
    double x,y;
};
double operator * (p a,p b)
{
    return a.x*b.y-a.y*b.x;
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int x,y;
    cin>>x>>y;
    if(x<y)
      cout<<-1<<endl;
    else if(x==y)
      printf("%.12f\n",double(x));
    else
    {
        int tmp=x/y;
        if(tmp%2==0)
          tmp--;
        p a,b;
        double ans,mid,l=y,r=2*y;
        while(r-l>eps)
        {
            mid=(l+r)/2;
            //cout<<l<<' '<<r<<' '<<mid<<endl;
            a.x=x-tmp*mid;
            a.y=y-mid;
            b.x=mid;
            b.y=-mid;
            ans=mid;
            if(a*b<0)
              l=mid;
            else if(a*b>eps)
              r=mid;
            else
            {
                break;
            }
        }
        printf("%.12f\n",ans);
    }
    return 0;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38313974/article/details/79966027
文章标签: cf 二分
个人分类: 二分三分
上一篇Codeforces Round #313 (Div. 2)C. Gerald's Hexagon
下一篇Codeforces Round #318 D. Bear and Blocks
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭