CF 578A 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.
思路:先大概确定范围,再二分。
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
#define INF 0x7fffffff
int main(){
int a,b,x,y;
double ans = INF;
while(scanf("%d%d",&x,&y) == 2){
if(y > x){
printf("-1\n");
continue;
}
else if(y == 0){
printf("0.000000000001\n");
continue;
}
b = x+y;
int k = 1;
int t ;
ans = INF ;
for(; ;){
t = k;
k *= 2;
if(b*1.0/k >= y)
ans = ans < b*1.0/k ? ans : b*1.0/k ;
else
break;
}
while(t<k){
int mid = (t+k)/2;
if(mid%2 == 1)
break;
if(b*1.0/mid >= y)
t = mid ;
else
k = mid ;
}
if(t == 1)
t = 2;
if(k == 1)
k = 2;
double s1 = b*1.0/k;
double s2 = b*1.0/t;
if(s1 >= y && s2 >= y)
ans = s1 > s2 ? s2 : s1 ;
else if(s1 >= y)
ans = s1;
else
ans = s2;
b = y -x ;
if(b<0)
b = -b;
if(b == 0){
printf("%.12lf\n",(double)y);
continue;
}
k = 1;
for(; ;){
t = k;
k *= 2;
if(b*1.0/k >= y)
ans = ans < b*1.0/k ? ans : b*1.0/k ;
else
break;
}
while(t<k){
int mid = (t+k)/2;
if(mid%2 == 1)
break;
if(b*1.0/mid >= y)
t = mid ;
else
k = mid ;
}
if(t == 1)
t = 2;
if(k == 1)
k = 2;
s1 = b*1.0/k;
s2 = b*1.0/t;
if(s1 >= y && s2 >= y){
double temp = s1 > s2 ? s2 : s1 ;
ans = temp;
}
else if(s1 >= y)
ans = ans > s1 ? s1 : ans;
else if(s2 >= y)
ans = ans > s2 ? s2 : ans;
printf("%.12lf\n",ans);
}
return 0;
}
CF 578A A Problem about Polyline的更多相关文章
- codeforces 578a//A Problem about Polyline// Codeforces Round #320 (Div. 1)
题意:一个等腰直角三角形一样的周期函数(只有x+轴),经过给定的点(a,b),并且半周期为X,使X尽量大,问X最大为多少? 如果a=b,结果就为b 如果a<b无解. 否则,b/(2*k*x-a) ...
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] C C Problem about Polyline 数学
C. A Problem about Polyline ...
- 【26.09%】【codeforces 579C】A Problem about Polyline
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- cf C. Tourist Problem
http://codeforces.com/contest/340/problem/C #include <cstdio> #include <cstring> #includ ...
- CF 291E. Tree-String Problem [dfs kmp trie图优化]
CF291E 题意:一棵树,每条边上有一些字符,求目标串出现了多少次 直接求目标串的fail然后一边dfs一边跑kmp 然后就被特殊数据卡到\(O(n^2)\)了... 因为这样kmp复杂度分析的基础 ...
- CF 1096D Easy Problem [动态规划]
题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 有一长度为n的字符串,每一字符都有一个权值,要求现在从中取出若干个字符,使得字符串中没 ...
- codeforces Round #320 (Div. 2) C. A Problem about Polyline(数学) D. "Or" Game(暴力,数学)
解题思路:就是求数 n 对应的二进制数中有多少个 1 #include <iostream> #include<cstdio> using namespace std; int ...
- CF 33B String Problem
对每个位置进行操作,求出最终变成相同字母的代价,然后把所有的位上代价加起来,使得总代价最小.res[i][j]表示将字母i+'a'和字母j+'a'变为相同的代价,设最终都变成字母k+'a',那么res ...
- 【CF contest/792/problem/E】
E. Colored Balls time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
随机推荐
- 【hihoCoder第十五周】最近公共祖先·二
老实说我没有读题,看见标题直接就写了,毕竟hiho上面都是裸的算法演练. 大概看了下输入输出,套着bin神的模板,做了个正反map映射,但是怎么都得不了满分.等这周结束后,找高人询问下trick. 若 ...
- JAVA获取oracle中sequences的最后一个值
项目中,用到一个序列作单号,框架用的是ssh,在dao层去拿的时候,运行时报错为dual is not mapped,[select *.nextval nextvalue from dual] 后来 ...
- ServletRequest中getReader()和getInputStream()只能调用一次的解决办法(转)
原文地址:http://liwx2000.iteye.com/blog/1542431 原文作者:liwx2000 为了提高项目安全性,拦截非法访问,要给项目增加了一个过滤器,拦截所有的请求,校验是否 ...
- 开源 免费 java CMS - FreeCMS1.5-系统配置
下载地址:http://code.google.com/p/freecms/ 系统配置 从FreeCMS 1.2 开始支持 管理系统使用的配置项. 从左侧管理菜单点击系统配置进入. 从FreeCMS ...
- 新建的硬盘-mount
一.查看已经格式化或已挂接硬盘 df -aT 等命令(自己度娘) 二.查看未挂硬盘和未格式化硬盘 1.fdisk -l 比如/dev/sdb 如果已经分区,会有sdb1. 2.格式化 #mkfs - ...
- java基础之代理
代理的定义,代理的应用,代理的特性
- .NET开发人员必须知道的八个网站
对于不熟悉.NET技术的朋友,需要说明一下,.NET提供了一个平台和一些相应的工具,.NET开发人员可以使用它们来在开发Windows桌面,互联网,甚至是手持移动设备上构建极富交互性的应用.很有可能你 ...
- SqlBulkCopy的一个例子
public bool InsertAll(IList<NewStockLuoPan> list) { DataTable dt = new DataTable(); dt.Columns ...
- Windows命令行(DOS命令)教程-4(转载)http://arch.pconline.com.cn//pcedu/rookie/basic/10111/15325_3.html
2. md md是英文make directory(创建目录)的缩写 [功能] 创建一个子目录 [格式] md [C:]path [举例] 用md 建立一个叫做purple的目录 3. cd cd是英 ...
- AngularJs练习Demo3
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...