zoukankan      html  css  js  c++  java
  • 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
    ",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
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    iPhone应用程序开发基础之一: IBOutlet与IBAction
    Swift实战-小QQ(第1章):QQ登录界面
    Swift实战-QQ在线音乐(AppleWatch版)
    iOS苹果官方Demo合集
    Git--Submodule使用
    线程审查生产者和消费者
    Lichee(三) Android4.0该产品的目标文件夹,Lichee链接---extract-bsp
    curl转让query string逃生参数
    ERROR 2003 (HY000): Can&#39;t connect to MySQL server on &#39;10.16.115.101&#39; (111)
    吐槽一下Activiti用户手册和一本书
  • 原文地址:https://www.cnblogs.com/acagain/p/9180715.html
Copyright © 2011-2022 走看看