wenbao与三分
--------------------------------------------------------------------
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3010
求n个函数在[0,1000]的最大值的最小值
1 #include <iostream> 2 using namespace std; 3 const int maxn = 10009; 4 int t, n; 5 double a[maxn], b[maxn], c[maxn]; 6 double f(double x){ 7 double ma; 8 for(int i = 0; i < n; ++i){ 9 double xx = a[i]*x*x + b[i]*x + c[i]; 10 if(i == 0) ma = xx; 11 else ma = max(ma, xx); 12 } 13 return ma; 14 } 15 int main(){ 16 scanf("%d", &t); 17 while(t--){ 18 scanf("%d", &n); 19 for(int i = 0; i < n; ++i){ 20 scanf("%lf%lf%lf", &a[i], &b[i], &c[i]); 21 } 22 double l = 0.0, r = 1000.0, lx, rx; 23 for(int i = 0; i < 100; ++i){ 24 lx = l + (r-l)/3.0; 25 rx = r - (r-l)/3.0; 26 if(f(lx) < f(rx)) r = rx; 27 else l = lx; 28 } 29 printf("%.4lf\n", f(l)); 30 } 31 return 0; 32 }
--------------------------------------------------------------------
http://poj.org/problem?id=3737
面积固定,求最大的体积
1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 #define PI acos(-1.0) 5 double s; 6 double geth(double r){ 7 return sqrt(pow(s/PI/r-r, 2.0) - pow(r, 2.0)); 8 } 9 double f(double r){ 10 return 1.0/3.0*PI*r*r*geth(r); 11 } 12 int main(){ 13 while(~scanf("%lf", &s)){ 14 double l = 0.00001, r = sqrt(s/PI/2.0)-0.00001; 15 for(int i = 0; i < 100; ++i){ 16 double lx = l + (r-l)/3.0, rx = r - (r-l)/3.0; 17 if(f(lx) > f(rx)) r = rx; 18 else l = lx; 19 } 20 printf("%.2lf\n%.2lf\n%.2lf\n", f(l), geth(l), l); 21 } 22 return 0; 23 }
--------------------------------------------------------------------
http://acm.hdu.edu.cn/showproblem.php?pid=2438
汽车拐弯
考虑极端情况。车右边贴墙走,若车左边蹭到墙则不能通过,所以求离墙最远近的点。设车与墙的夹角为xx,则离右边墙的距离可以用l*cos(xx)+w/sin(xx)-x/tan(xx)表示出来,三分求最大值
1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 #define PI acos(-1.0) 5 double x, y, l, w; 6 double f(double xx){ 7 return l*cos(xx)+w/sin(xx) - x/tan(xx); 8 } 9 int main(){ 10 while(~scanf("%lf%lf%lf%lf", &x, &y, &l, &w)){ 11 double l = 0.00001, r = PI/2.0; 12 for(int i = 0; i < 100; ++i){ 13 double lx = l + (r-l)/3.0, rx = r - (r-l)/3.0; 14 if(f(lx) > f(rx)) r = rx; 15 else l = lx; 16 } 17 printf("%s\n", f(l) > y ? "no" : "yes"); 18 } 19 return 0; 20 }
--------------------------------------------------------------------
http://acm.hdu.edu.cn/showproblem.php?pid=3400
1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 double xa, ya, xb, yb, xc, yc, xd, yd, p, q, r, Mi; 5 double dis(double x1, double y1, double x2, double y2){ 6 return sqrt(1.0*(x1-x2)*(x1-x2)+1.0*(y1-y2)*(y1-y2)); 7 } 8 double ff(double x, double y, double xx, double yy){ 9 return dis(x, y, xx, yy)/r + dis(xx, yy, xd, yd)/q; 10 } 11 double f(double x, double y){ 12 double sum = dis(xa, ya, x, y)/p; 13 double lx = xd, ly = yd, rx = xc, ry = yc; 14 for(int i = 0; i < 100; ++i){ 15 double llx = lx + (rx-lx)/3.0, lly = ly + (ry-ly)/3.0; 16 double rrx = rx - (rx-lx)/3.0, rry = ry - (ry-ly)/3.0; 17 if(ff(x, y, llx, lly) > ff(x, y, rrx, rry)) lx = llx, ly = lly; 18 else rx = rrx, ry = rry; 19 } 20 double sum2 = ff(x, y, lx, ly); 21 if(sum+sum2 < Mi) Mi = sum + sum2; 22 return sum + sum2; 23 } 24 int main(){ 25 int t; 26 scanf("%d", &t); 27 while(t--){ 28 Mi = 10000000000.0; 29 scanf("%lf%lf%lf%lf", &xa, &ya, &xb, &yb); 30 scanf("%lf%lf%lf%lf", &xc, &yc, &xd, &yd); 31 scanf("%lf%lf%lf", &p, &q, &r); 32 double lx = xa, ly = ya, rx = xb, ry = yb; 33 for(int i = 0; i < 100; ++i){ 34 double llx = lx + (rx-lx)/3.0, lly = ly + (ry-ly)/3.0; 35 double rrx = rx - (rx-lx)/3.0, rry = ry - (ry-ly)/3.0; 36 if(f(llx, lly) > f(rrx, rry)) lx = llx, ly = lly; 37 else rx = rrx, ry = rry; 38 } 39 printf("%.2lf\n", Mi); 40 } 41 return 0; 42 }
--------------------------------------------------------------------
--------------------------------------------------------------------
--------------------------------------------------------------------
--------------------------------------------------------------------
--------------------------------------------------------------------
只有不断学习才能进步!