#include<bits/stdc++.h> usingnamespace std; constdouble eps = 1e-8; doublef(double x, double y) { return (x - 1) * (x - 1) + (x + y) * (x + y) + sin(y); } doublerun(double x)// 固定x,三分y { double l = -4, r = 4, mid; while (r - l > eps) { mid = (l + r) / 2; if (f(x, mid - eps) > f(x, mid + eps)) l = mid; else r = mid; } returnf(x, mid); } intmain() { double l = -4, r = 4, mid, ans; while (r - l > eps) { mid = (l + r) / 2; // 三分x if (run(mid - eps) > run(mid + eps)) l = mid; else r = mid; } printf("%.6f\n", run(mid)); // -0.918827 return0; }
配套题目
A - Texas Trip
After a day trip with his friend Dick, Harry noticed a strange
pattern of tiny holes in the door of his SUV. The local American Tire
store sells fiberglass patching material only in square sheets. What is
the smallest patch that Harry needs to fix his door?
Assume that the holes are points on the integer lattice in the plane.
Your job is to find the area of the smallest square that will cover all
the holes.
Input
The first line of input contains a single integer T
expressed in decimal with no leading zeroes, denoting the number of test
cases to follow. The subsequent lines of input describe the test
cases.
Each test case begins with a single line, containing a single integer
n expressed in decimal with no leading zeroes, the number of
points to follow; each of the following n lines contains two
integers x and y, both expressed in decimal with no
leading zeroes, giving the coordinates of one of your points.
You are guaranteed that T ≤ 30 and that no data set contains
more than 30 points. All points in each data set will be no more than
500 units away from (0,0).
Output
Print, on a single line with two decimal places of precision, the
area of the smallest square containing all of your points.
// by Totoro /* while (r - l > eps) { mid = (l + r) / 2; double fl = f(mid - eps), fr = f(mid + eps); if (fl < fr) l = mid; // 这里不写成mid - eps,防止死循环;可能会错过极值,但在误差范围以内所以没关系 else r = mid; } */ /* double run(double x) // 固定x,三分y { double l = -4, r = 4, mid; while (r - l > eps) { mid = (l + r) / 2; if (f(x, mid - eps) > f(x, mid + eps)) l = mid; else r = mid; } return f(x, mid); } */ #include<iostream> #include<algorithm> #include<cmath> #include<stdio.h> usingnamespace std; #define rep(i, a, n) for (int i = a; i <= n; i++) #define frep(i, a, n) for (int i = a; i >= n; i--) #define ll long long int n; double inf=1e10; constint N=1e6+100; double x[N]; double y[N]; constdouble eps = 1e-8; //精度 constdouble pi = acos(-1.0); //求pi doublef(double t) { double maxx = -inf, minx = inf, maxy = -inf, miny = inf; for (int i = 0; i < n; ++i) { maxx = max(maxx, x[i] * cos(t) - y[i] * sin(t)); maxy = max(maxy, x[i] * sin(t) + y[i] * cos(t)); miny = min(miny, x[i] * sin(t) + y[i] * cos(t)); minx = min(minx, x[i] * cos(t) - y[i] * sin(t)); //求出最大最小 } returnmax(maxx - minx, maxy - miny); } intmain() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { cin >> n; rep(i, 0, n - 1) { cin >> x[i] >> y[i]; } double l = 0; double r = pi ; double mid; while (r - l > eps) { mid = (l + r) / 2; if (f(mid - eps) > f(mid + eps)) l = mid; else r = mid; } double ans = f(l); printf("%.2f\n", ans * ans); //输出面积 } }
B - Line belt
In a two-dimensional plane there are two line belts, there are two
segments AB and CD, lxhgww's speed on AB is P and on CD is Q, he can
move with the speed R on other area on the plane. How long must he take
to travel from A to D?
Input
The first line is the case number T. For each case, there are three
lines. The first line, four integers, the coordinates of A and B: Ax Ay
Bx By. The second line , four integers, the coordinates of C and D:Cx Cy
Dx Dy. The third line, three integers, P Q R. 0<=
Ax,Ay,Bx,By,Cx,Cy,Dx,Dy<=1000 1<=P,Q,R<=10
Output
The minimum time to travel from A to D, round to two decimals.
// by Totoro /* while (r - l > eps) { mid = (l + r) / 2; double fl = f(mid - eps), fr = f(mid + eps); if (fl < fr) l = mid; // 这里不写成mid - eps,防止死循环;可能会错过极值,但在误差范围以内所以没关系 else r = mid; } */ /* double run(double x) // 固定x,三分y { double l = -4, r = 4, mid; while (r - l > eps) { mid = (l + r) / 2; if (f(x, mid - eps) > f(x, mid + eps)) l = mid; else r = mid; } return f(x, mid); } */ #include<bits/stdc++.h> usingnamespace std; #define rep(i, a, n) for (int i = a; i <= n; i++) #define frep(i, a, n) for (int i = a; i >= n; i--) using ll = longlong; double p, q, r; double x_1, x2, y_1, y2; double x3, x4, y3, y4; double eps = 1e-8; doublejuli(double x_1,double y_1,double x2,double y2) { returnsqrt((x_1-x2)*(x_1-x2)+(y_1-y2)*(y_1-y2)); } doublef(double x,double y) { double x5,x6,y5,y6; x5=(x2-x_1)*x+x_1; y5=(y2-y_1)*x+y_1; x6=(x4-x3)*(1-y)+x3; y6=(y4-y3)*(1-y)+y3; double a=juli(x_1,y_1,x5,y5)/p; double b=juli(x5,y5,x6,y6)/r; double c=juli(x6,y6,x4,y4)/q; double ans=a+b+c; return ans; } doublerun(double x) //占比为x //已经固定了x了 { double l=0; double r=1; double mid; while(r-l>eps) { mid=(l+r)/2; if(f(x,mid-eps)>f(x,mid+eps)) { l=mid; } else { r=mid; } } returnf(x,mid); } voidsolve() { //本题有两条线段 //AB,CD //显然路径要怎么走呢 //从A-E-F-D //显然是这样 //根据三分的话 //应该是先三分E,再三分F //三分AE在AB上的占比 //占比的枚举应该是从0-1开始的 //再三分CF在CD上的占比 //应该就可以做 //试一试 cin>>x_1>>y_1>>x2>>y2; cin>>x3>>y3>>x4>>y4; cin>>p>>q>>r; double l=0; double r=1; double mid; while(r-l>eps) { mid=(r+l)/2; if(run(mid-eps)>run(mid+eps)) { l=mid; } else { r=mid; } } printf("%.2lf",run(mid)); } intmain() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t = 1; cin>>t; while (t--) { solve(); } }
C - Stealing a Cake
There is a big round cake on the ground. A small ant plans to steal a
small piece of cake. He starts from a certain point, reaches the cake,
and then carry the piece back home. He does not want to be detected, so
he is going to design a shortest path to achieve his goal.
The big cake can be considered as a circle on a 2D plane. The ant’s
home can be considered as a rectangle. The ant can walk through the
cake. Please find out the shortest path for the poor ant.
Input
The input consists of several test cases. The first line of each test
case contains x,y, representing the coordinate of the starting point.
The second line contains x, y, r. The center of the cake is point (x,y)
and the radius of the cake is r. The third line contains x1,y1,x2,y2,
representing the coordinates of two opposite vertices of the rectangle
--- the ant's home. All numbers in the input are real numbers range from
-10000 to 10000. It is guaranteed that the cake and the ant's home don't
overlap or contact, and the ant's starting point also is not inside the
cake or his home, and doesn't contact with the cake or his home. If the
ant touches any part of home, then he is at home. Input ends with a line
of 0 0. There may be a blank line between two test cases.
Output
For each test case, print the shortest distance to achieve his goal.
Please round the result to 2 digits after decimal point.