bzoj 1857

 

三分,对于单凸的函数(单调的也可以),可以找出最值。

这道题可以感性认识一下。。。。。。

 

 1 /**************************************************************
 2     Problem: 1857
 3     User: idy002
 4     Language: C++
 5     Result: Accepted
 6     Time:32 ms
 7     Memory:1272 kb
 8 ****************************************************************/
 9  
10 #include <cstdio>
11 #include <cmath>
12 #include <iostream>
13 #define eps 1e-5
14 using namespace std;
15  
16 int sg( double x ) { return (x>-eps)-(x<eps); }
17 struct Vector {
18     double x, y;
19     Vector(){}
20     Vector( double x, double y ):x(x),y(y){}
21     Vector operator+( const Vector & b ) const { return Vector(x+b.x,y+b.y); }
22     Vector operator-( const Vector & b ) const { return Vector(x-b.x,y-b.y); }
23     Vector operator*( double k ) const { return Vector(x*k,y*k); }
24     void read() { scanf( "%lf%lf", &x, &y ); }
25     double dis() { return sqrt(x*x+y*y); }
26 };
27 typedef Vector Point;
28  
29 Point A, B, C, D, E1, E2, E, F1, F2, F;
30 double P, Q, R;
31  
32 inline double gettime() {
33     return (A-E).dis()/P+(F-D).dis()/Q+(E-F).dis()/R;
34 }
35 double onCD() {
36     double t1, t2;
37     F1 = C, F2 = D;
38     while( (F1-F2).dis() >= eps ) {
39         Vector step = (F2-F1)*(1.0/3.0);
40         F = F1+step;
41         t1 = gettime();
42         F = F +step;
43         t2 = gettime();
44         if( t1-t2<0.0 ) {
45             F2 = F2-step;
46         } else {
47             F1 = F1+step;
48         }
49     }
50     F = F1;
51     return gettime();
52 }
53 double onAB() {
54     double t1, t2;
55     E1 = A, E2 = B;
56     while( (E1-E2).dis() >= eps ) {
57         Vector step = (E2-E1)*(1.0/3.0);
58         E = E1+step;
59         t1 = onCD();
60         E = E +step;
61         t2 = onCD();
62         if( t1-t2<0.0 ) {
63             E2 = E2-step;
64         } else {
65             E1 = E1+step;
66         }
67     }
68     E = E1;
69     return onCD();
70 }
71 int main() {
72     A.read();
73     B.read();
74     C.read();
75     D.read();
76     scanf( "%lf%lf%lf", &P, &Q, &R );
77     printf( "%.2lf\n", onAB() );
78 }
View Code

 

posted @ 2015-03-31 20:47  idy002  阅读(179)  评论(0编辑  收藏  举报