Description
Unlike most students of the Mathematical Department, Sonya is fond of not only programming but also sports. One fine day she went to play football with her friends. Unfortunately, there was no football field anywhere around.
There only was a lonely birch tree in a corner of the yard. Sonya searched the closet at her home, found two sticks, and decided to construct a football goal using the sticks and the tree. Of course, the birch would be one of the side posts of the goal. It
only remained to make the other post and the crossbar.
Sonya wanted to score as many goals as possible, so she decided to construct a goal of maximum area. She knew that the standard football goal was rectangular, but, being creative, she assumed that her goal could have the form
of an arbitrary quadrangle.
You can assume that the birch tree is a segment of a straight line orthogonal to the ground.
Input
The only line contains integers a and b, which are the lengths of the sticks (1 ≤ a, b ≤ 10 000). It is known that the total length of the sticks is less than the height of the birch tree.
Output
Output the maximum area of the goal that can be constructed with the use of the sticks and the birch tree. The answer must be accurate to at least six fractional digits.
Sample Input
input | output |
---|---|
2 2 |
4.828427125 |
|
|
|
|__________________ 找两个杆子来围住左边这个 使得面积最大;
初始想法是枚举角度;尽管精度感觉都对了但是还是WA
错误的代码:
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> using namespace std; #define PI acos(-1.0) #define SET(a,b) memset(a,b,sizeof(a)) #define DE(x) cout<<#x<<"="<<x<<endl //308.812191 int main(){ double x,y; double sum=0; while(~scanf("%lf%lf",&x,&y)){ sum=x*y; double now; double p=PI/2000.0; // double p2=p1; for(int i=1;i<=2000;i++){ double x1=x*sin(i*p); double x2=x*cos(i*p); for(int j=1;j<=2000-i;j++){ double y1=y*sin(j*p); double y2=y*cos(j*p); now=x1*x2/2.0+y1*y2/2.0+x2*y2; if(now>sum)sum=now; } } printf("%.6lf",sum); } return 0; }
后面
利用2*ac*bc<=ac^2+bc^2=ab^2 三角形abd可以利用海伦公式,三角形abc=1/2 ac*cb 最大就是ab^2/4
然后三分 0到x+y 就出来了
三分的模板:
double solve() { double Left, Right; double mid, midmid; double mid_value, midmid_value; Left = 0; Right = x+y; while (Left + eps <= Right) { mid = (Left + Right) / 2.0; midmid = (mid + Right) / 2.0; mid_value=getsum(mid,x,y); midmid_value=getsum(midmid,x,y); if (mid_value>=midmid_value) Right = midmid; else Left = mid; } return mid_value; }
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #define eps 1e-9 using namespace std; //308.812191 double getsum(double c,double a,double b){ double p=(a+b+c)/2.0; return c*c/4.0+sqrt(p*(p-a)*(p-b)*(p-c)); } double x,y; double solve() { double Left, Right; double mid, midmid; double mid_value, midmid_value; Left = 0; Right = x+y; while (Left + eps <= Right) { mid = (Left + Right) / 2.0; midmid = (mid + Right) / 2.0; mid_value=getsum(mid,x,y); midmid_value=getsum(midmid,x,y); if (mid_value>=midmid_value) Right = midmid; else Left = mid; } return mid_value; } int main(){ while(~scanf("%lf%lf",&x,&y)){ printf("%.9lf ",solve()); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。