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

来源:互联网 发布:万捷网络验证系统 编辑:程序博客网 时间:2024/04/30 20:53

题目链接:点击打开链接


有一条折线,转折点是(0, 0) - (x, x) - (2x, 0) - (3x, x) - ... - (2kx, 0) - (2kx + x, x),给你一个点(a, b)在折线上面,问你最小的x是多少,

若不存在x则输出-1。


首先要考虑输出-1的情况,也就是b > a的时候,无法构成一个等腰直角三角形。想到平移斜率不同的线段最终可以确定两条直线的公

式:y = x - 2 * x1 * x0 与 y = -x + 2 * x2 * x0(x0为题目中每个转折点的x)。化简即得x0 = (x + y) / (2 * x2), x0 = (x - y) / (2 * x1),取最小即可。


AC代码:


#include "iostream"#include "cstdio"#include "cstring"#include "algorithm"using namespace std;int main(int argc, char const *argv[]){int x, y;scanf("%d%d", &x, &y);if(y > x) {printf("-1\n");return 0;}double x1 = (x - y) / (2 * y), x2 = (x + y) / (2 * y);printf("%.12f\n", min((x + y) / (2 * x2), (x - y) / (2 * x1)));return 0;}


1 0