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

原创 2018年04月16日 20:32:23

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

【 Codeforces Round #462 (Div. 2)】C - A Twisty Movement

题目链接 题意:一个长为n(&amp;lt;=2000)只由1或2组成的序列,可以将一段区间【l,r】的元素反转, 求反转后的非递减子序列的最长长度 int main() { int ...
  • feng_zhiyu
  • feng_zhiyu
  • 2018-02-14 23:52:47
  • 123

Educational Codeforces Round 38 (Rated for Div. 2) B. Run For Your Prize

B. Run For Your PrizeYou and your friend are participating in a TV show &quot;Run For Your Prize&quo...
  • s540239976
  • s540239976
  • 2018-02-17 13:35:37
  • 213

Codeforces Round #457 (Div. 2) B. Jamie and Binary Sequence(二进制,思路,贪心)

描述 Jamie is preparing a Codeforces round. He has got an idea for a problem, but does not know...
  • riba2534
  • riba2534
  • 2018-01-20 15:23:28
  • 367

Codeforces Round #465 (Div. 2) C. Fifa and Fafa(圆问题,数论)

C. Fifa and Fafatime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutp...
  • qq_34826781
  • qq_34826781
  • 2018-02-20 16:16:48
  • 141

Codeforces Round #459 (Div. 2) C. The Monster

题目链接 C. The Monster time limit per test 1 second memory limit per test 256 megabytes input s...
  • ffgcc
  • ffgcc
  • 2018-01-30 09:48:19
  • 106

Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence (构造 贪心)

1000个数,每个数不超过1e6,让你构造一个不超过4000的数列,使得对于新数列,任意区间内的数求GCD都出现在这1000个数中。...
  • hmc0411
  • hmc0411
  • 2017-11-20 13:19:26
  • 435

Codeforces Round #471 div.2 ABC题解

其实我是震惊的 A B C 其实我是震惊的 我被A题卡住了,21分钟才过.B题倒是挺快,29分钟.然后我一直想C和D. C是个数论题我觉得还能做,D我不会. 但是我发现1个...
  • qq_31908675
  • qq_31908675
  • 2018-03-24 14:16:58
  • 75

Codeforces Round #465 (Div. 2) C. Fifa and Fafa

题目链接:点击打开链接题意:给定一个圆位于(x1, y1),半径为R,再给定平面上任意一点(x2, y2),在圆内找一点画一个半径为r的圆,使得该圆将该点包含在园内(可以在边界)且使得大圆内不属于小圆...
  • Lfhase
  • Lfhase
  • 2018-02-20 04:24:46
  • 138

Codeforces Round #327 (div.2)(A B C D)

codeforces round #327 (div.2)
  • lincifer
  • lincifer
  • 2015-10-26 13:11:58
  • 1197

Codeforces Round #439 (Div. 2) A-C题解

A. The Artful Expedient time limit per test1 second memory limit per test256 megabytes inputstandard...
  • Ever_glow
  • Ever_glow
  • 2017-10-07 09:50:55
  • 928
收藏助手
不良信息举报
您举报文章:Codeforces Round #320 (Div. 2)C. A Problem about Polyline
举报原因:
原因补充:

(最多只允许输入30个字)